2

以下のコードを使用して投稿しようとしています。トークンを返すことを期待していますが、エラー405メソッドが許可されていません

<cfhttp method="POST" url="http://accounts.google.com/o/oauth2/token" >
    <cfhttpparam type="Formfield" name="code" value="#url.CODE#">
    <cfhttpparam type="Formfield" name="client_id" value="458381219741.apps.googleusercontent.com">
    <cfhttpparam type="Formfield" name="client_secret" value="XXXXXXX">
    <cfhttpparam type="Formfield" name="redirect_uri" value="http://console.mbwebportal.com/oauth2callback">
    <cfhttpparam type="Formfield" name="grant_type" value="authorization_code">
</cfhttp>

上記のコードは http://console.mbwebportal.com/oauth2callbackにあり、ユーザーがアプリケーションへのアクセスを許可した後、URLでコードを取得します。

助けてください!!

4

2 に答える 2

2

私は答えを見つけました:重要なのはcfhttpparamtype'body'を使用することでし。livedocsによると、「body:HTTPリクエストの本文を指定します。ColdFusionはcontent-typeヘッダーを自動的に設定したり、本文のコンテンツをURLエンコードしたりしません。content-typeを指定するには、type=headerで別のcfhttpタグを使用します。」

以下のコードは今私にアクセストークンを返しています:)

<cfset client_id = "458381219741.apps.googleusercontent.com">
<cfset client_secret = "**********">
<cfset callback = "http://console.mbwebportal.com/oauth2callback">

<cfset postBody = "code=" & UrlEncodedFormat(url.code) & "&">
<cfset postBody = postBody & "client_id=" & UrlEncodedFormat(client_id) & "&">
<cfset postBody = postBody & "client_secret=" & UrlEncodedFormat(client_secret) & "&">
<cfset postBody = postBody & "redirect_uri=" & UrlEncodedFormat(callback) & "&">
<cfset postBody = postBody & "grant_type=authorization_code">
<cfhttp method="post" url="https://accounts.google.com/o/oauth2/token">
    <cfhttpparam name="Content-Type" type="header" value="application/x-www-form-urlencoded">
    <cfhttpparam type="body" value="#postBody#">
</cfhttp>
于 2012-11-09T19:21:23.333 に答える
0

ここで同様の投稿が見つかりましたGoogleOAuth2認証-トークンのコードを交換します。彼らの答えは、クライアントの秘密鍵をURLエンコードし、URIをリダイレクトすることでした。ColdFusionでは、このURLEncodedFormat()関数を使用してそれを行うことができます。

<cfhttp method="POST" url="http://accounts.google.com/o/oauth2/token" >
    <cfhttpparam type="Formfield" name="code" value="#url.CODE#">
    <cfhttpparam type="Formfield" name="client_id" value="458381219741.apps.googleusercontent.com">
    <cfhttpparam type="Formfield" name="client_secret" value="#URLEncodedFormat(XXXXXXX)#">
    <cfhttpparam type="Formfield" name="redirect_uri" value="#URLEncodedFormat("http://console.mbwebportal.com/oauth2callback")#">
    <cfhttpparam type="Formfield" name="grant_type" value="authorization_code">
</cfhttp>

url.CODEまた、URLで何でも渡すことができるため、使用する前に値を検証してください。

于 2012-11-08T13:16:32.300 に答える