【问题标题】:Google oauth token giving 405 error谷歌 oauth 令牌给出 405 错误
【发布时间】:2012-10-28 15:27:28
【问题描述】:
我正在尝试使用以下代码发布。我希望它返回令牌,但它返回 error 405 Method Not Allowed。
<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中获取代码。
请帮忙!!
【问题讨论】:
标签:
coldfusion
oauth-2.0
http-status-code-405
【解决方案1】:
我找到了答案:关键是使用 cfhttpparam type '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>
【解决方案2】:
在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 中传递。