【问题标题】: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中获取代码。

请帮忙!!

【问题讨论】:

  • 您不应该在请求中使用 httpS 协议吗?来自 Google 的 developers.google.com/accounts/docs/OAuth2WebServer 页面:“此请求是一个 HTTPS 帖子,包含以下参数...”
  • no https 给出一些其他错误(例如找不到页面)。根据谷歌文档,它必须是 HTTP 1.1 请求,它会自动调用 https 地址。尝试在浏览器中粘贴accounts.google.com/o/oauth2/token,您会看到它转换为https。

标签: 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 中传递。

    【讨论】:

    • 它没有解决同样的问题。此外,如果我只使用 1 cfhttpparam 执行 cfhttp,它会给出类似的错误。到目前为止,我可以 url accounts.google.com/o/oauth2/token 甚至没有查看发送的参数。它只是不喜欢数据发布的方式:(
    猜你喜欢
    • 2013-07-04
    • 2016-11-25
    • 2015-05-17
    • 2014-07-24
    • 2016-10-09
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多