我正试图使用下面的代码发布。我希望它返回令牌,但它返回的错误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中获取代码。
救命啊!!
发布于 2012-11-09 19:21:23
我找到了答案:关键是使用cfhttpparam类型'body‘。根据livedocs " body :指定HTTP的主体。ColdFusion不会自动设置内容类型的标题或URL对正文内容进行编码。要指定内容类型,请使用单独的带有type=header的cfhttp标记。“
下面的代码现在正在返回me access令牌:)
<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-08 13:16:32
在这里发现了一个类似的帖子,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.CODE值,因为任何东西都可以在URL中传递。
https://stackoverflow.com/questions/13288949
复制相似问题