【问题标题】:Google OAuth Code Contains HashtagGoogle OAuth 代码包含 Hashtag
【发布时间】:2015-06-26 12:50:59
【问题描述】:

在我尝试获取必要的代码以便生成刷新令牌时,我运行了这个 URL:

https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/drive.file&redirect_uri=--mywebsite--&response_type=code&client_id=--myclientid--

并且我收到的代码包含一个尾随哈希标记,当我尝试执行时会引发(预期的)错误:

<cfhttp url="https://accounts.google.com/o/oauth2/token" method="post">
    <cfhttpparam name="code" value="4/UXF5F5TlIuFsXrav-DvIrebMR8NST9WK-EPmThmx7l0#" type="formfield"> <!-- Sample code value -->
    <cfhttpparam name="client_id" value="--myclientid--" type="formfield">
    <cfhttpparam name="client_secret" value="[client secret]" type="formfield">
    <cfhttpparam name="redirect_uri" value="--mywebsite--">
    <cfhttpparam name="grant_type" value="authorization_code" type="formfield">
</cfhttp>

ColdFusion 正在查看以下文本:

表单域

CFML 编译器正在处理:

  • 从第 2 行第 86 列开始的表达式。
    表达式可能缺少结尾 #,例如,#expr 而不是 #expr#。
  • 标签属性值,第 2 行,第 34 列。
  • 从第 2 行第 10 列开始的 cfhttpparam 标记。

我尝试添加第二个主题标签以使其文字化,但我收到了{ "error" : "invalid_grant", "error_description" : "Invalid code." } 响应。

我在这里遗漏了一些非常明显的东西吗?我关注的教程在http://www.brandiandjohn.com/post.cfm/oauth-2-google-and-cfml-without-cfoauth

【问题讨论】:

  • 我认为这可能是一个红鲱鱼。您将主题标签加倍是正确的。这将使 ColdFusion 向 Google 发送一个标签。我的猜测是这里还有其他问题。
  • 你是对的。我确实找到了解决方案。

标签: coldfusion oauth-2.0 google-drive-api coldfusion-10


【解决方案1】:

为了继续使用现有代码,您需要转义 #。您可以通过在末尾添加一个额外的 # 来做到这一点。例如

<!--- 
    <cfset value="4/UXF5F5TlIuFsXrav-DvIrebMR8NST9WK-EPmThmx7l0#">
    <cfoutput>#value#</cfoutput>
    Error
 --->
<cfset value="4/UXF5F5TlIuFsXrav-DvIrebMR8NST9WK-EPmThmx7l0##">
<cfoutput>#value#</cfoutput>
Output: 4/UXF5F5TlIuFsXrav-DvIrebMR8NST9WK-EPmThmx7l0#

所以,你可以试试下面的代码:-

<cfhttp url="https://accounts.google.com/o/oauth2/token" method="post">
        <cfhttpparam name="code" value="4/UXF5F5TlIuFsXrav-DvIrebMR8NST9WK-EPmThmx7l0##" type="formfield"> <!-- Sample code value -->
        <cfhttpparam name="client_id" value="--myclientid--" type="formfield">
        <cfhttpparam name="client_secret" value="[client secret]" type="formfield">
        <cfhttpparam name="redirect_uri" value="--mywebsite--">
        <cfhttpparam name="grant_type" value="authorization_code" type="formfield">
    </cfhttp>

【讨论】:

  • 我试过了,正如我帖子底部附近所说的那样。
【解决方案2】:

主题标签错误是一个红鲱鱼。我通过调用获取代码然后将访问令牌作为单个操作来解决这个问题。通过传入 url.code 我收到了必要的凭据。

  <cfhttp url="https://accounts.google.com/o/oauth2/token" method="post">
        <cfhttpparam name="code" value="#url.code#" type="formfield">
        <cfhttpparam name="client_id" value="--myclientid--" type="formfield">
        <cfhttpparam name="client_secret" value="[client secret]" type="formfield">
        <cfhttpparam name="redirect_uri" value="--mywebsite--">
        <cfhttpparam name="grant_type" value="authorization_code" type="formfield">
    </cfhttp>

<cfdump var="#foo.filecontent#">

手动将代码粘贴到 cfhttpparam 标记中,即使没有主题标记,也会引发 400 错误。这样就不行了。

【讨论】:

  • 请谨慎使用 url 范围内的任何内容,而无需先对其进行验证/清理。
【解决方案3】:

向您发送code 的用户代理应该去掉了“#”字符,请参阅:Google OAuth service redirects to URL with a # sign at the end。显然它没有(因此用户代理已损坏),但如果关闭,您可以在发送之前在代码中手动剥离它。

【讨论】:

  • 剥离角色没有帮助,但我可以让它工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-23
  • 1970-01-01
  • 2017-02-15
  • 2013-09-23
  • 1970-01-01
  • 2014-09-13
  • 2023-03-24
相关资源
最近更新 更多