首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >提供405错误的Google令牌

提供405错误的Google令牌
EN

Stack Overflow用户
提问于 2012-11-08 12:15:30
回答 2查看 2.1K关注 0票数 2

我正试图使用下面的代码发布。我希望它返回令牌,但它返回的错误405方法不允许

代码语言:javascript
复制
<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中获取代码。

救命啊!!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-09 19:21:23

我找到了答案:关键是使用cfhttpparam类型'body‘。根据livedocs " body :指定HTTP的主体。ColdFusion不会自动设置内容类型的标题或URL对正文内容进行编码。要指定内容类型,请使用单独的带有type=header的cfhttp标记。“

下面的代码现在正在返回me access令牌:)

代码语言:javascript
复制
<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
EN

Stack Overflow用户

发布于 2012-11-08 13:16:32

在这里发现了一个类似的帖子,Google OAuth 2 authorization - swapping code for token。他们的答案是对客户端秘密密钥进行url编码并重定向uri。在ColdFusion中,您可以使用URLEncodedFormat()函数为您执行此操作。

代码语言:javascript
复制
<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中传递。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13288949

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档