ColdFusion Google OAuth 获取访问令牌连接失败。

3

参考Ray Camden的示例。一切都很顺利,直到我尝试获取访问令牌时。我的getAccessToken()函数出现了“连接失败”的错误。无法弄清楚我做错了什么?

<cfset authurl = "https://accounts.google.com/o/oauth2/v2/auth?" & 
"client_id=#urlEncodedFormat(application.clientid)#" & 
"&redirect_uri=#urlEncodedFormat(application.callback)#" & 
"&scope=https://www.googleapis.com/auth/userinfo.profile&response_type=code"> 
<cfoutput> 
authurl=#authurl#
<p><a href="#authurl#">Login</a></p> 
</cfoutput>

<cffunction name="getAccessToken"> 
<cfargument name="code" required="false" default="" type="string">
<cfset var postBody = "code=" & UrlEncodedFormat(arguments.code) & "&"> 
<cfset postBody = postBody & "client_id=" & UrlEncodedFormat(this.clientid) & "&"> 
<cfset postBody = postBody & "client_secret=" & UrlEncodedFormat(this.clientsecret) & "&"> 
<cfset postBody = postBody & "redirect_uri=" & UrlEncodedFormat(this.callback) & "&"> 
<cfset postBody = postBody & "grant_type=authorization_code">

<cfhttp method="post" url="https://www.googleapis.com/oauth2/v4/token"> 
<cfhttpparam type="header" name="Content-Type"  value="application/x-www-form-urlencoded"> 
<cfhttpparam type="body" value="#postBody#">     
</cfhttp>   
<cfreturn deserializeJSON(cfhttp.filecontent.tostring())>

2
你能从 ColdFusion 服务器访问 https://www.googleapis.com 吗? - Miguel-F
1
这很可能与使用cfhttp通过SSL(https)连接有关。请查看此答案https://dev59.com/Ouo6XIcBkEYKwwoYKRLd#20475763,以获取详细的解释和祝你好运! - JSONaLeo
谢谢你们俩!你们提供的链接中的说明解决了问题! - bernster
1个回答

0

替换这些行

<cfset postBody = postBody & "client_id=" & UrlEncodedFormat(this.clientid) & "&"> 
<cfset postBody = postBody & "client_secret=" & UrlEncodedFormat(this.clientsecret) & "&"> 
<cfset postBody = postBody & "redirect_uri=" & UrlEncodedFormat(this.callback) & "&"> 

使用

<cfset postBody = postBody & "client_id=" & UrlEncodedFormat(application.clientid) & "&"> 
<cfset postBody = postBody & "client_secret=" & UrlEncodedFormat(application.clientsecret) & "&"> 
<cfset postBody = postBody & "redirect_uri=" & UrlEncodedFormat(application.callback) & "&"> 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接