2012-11-08 78 views
2

我想使用下面的代碼發佈。我期望它返回令牌,但返回錯誤405方法不允許谷歌oauth令牌給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中的用戶允許訪問該應用程序後。

請幫忙!!

+0

你不應該在你的請求中使用HTTPS協議?來自Google的https://developers.google.com/accounts/docs/OAuth2WebServer頁面:「此請求是HTTP郵件,幷包含以下參數...」 – azawaza

+0

沒有https給出其他錯誤(如未找到頁面)。它必須是根據谷歌文檔的HTTP 1.1請求,它會自動調用https地址。嘗試在瀏覽器中粘貼http://accounts.google.com/o/oauth2/token,您會看到它轉換爲https。 –

回答

2

我找到了答案:關鍵是使用cfhttpparam類型'正文'。 根據livedocs「body:指定HTTP請求的主體,ColdFusion不會自動設置內容類型標頭或URL編碼正文內容。要指定內容類型,請使用帶有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> 
0

在這裏找到了一個類似的帖子Google OAuth 2 authorization - swapping code for token。他們的答案是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中傳遞之前驗證您url.CODE值。

+0

它沒有解決同樣的問題。另外如果我只是用1 cfhttpparam做cfhttp,它會給出類似的錯誤。到目前爲止,我可以通過http://accounts.google.com/o/oauth2/token網址查看發送的參數。它只是不喜歡數據後的方式:( –