2012-11-08 76 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网址查看发送的参数。它只是不喜欢数据后的方式:( –