2015-10-16 111 views
3

我正在嘗試將ColdFusion與Google+集成。我遇到grantType問題。首先,我使用Google對用戶進行了身份驗證。然後,當我試圖獲取令牌時,會拋出錯誤。下面是我的代碼:谷歌登錄與ColdFusion集成

<cfset code = URLEncodedFormat('#code#')> 
<cfset appId = URLEncodedFormat('#this.appId#')> 
<cfset key = URLEncodedFormat('#this.key#')> 
<cfset uri = URLEncodedFormat('#this.redirectURI#')> 
<cfset grant = 'authorization_code'> 
<cfset postBody = "code=#code#" 
       & "&client_id=#appId#" 
       & "&client_secret=#key#" 
       & "&redirect_uri=#uri#" 
       & "&grant_type=#grant#"> 
<cfhttp url = "https://accounts.google.com/o/oauth2/token" method="POST"> 
    <cfhttpparam type="body" value="#postBody#"> 
    <cfhttpparam type="header" name="Conternt-Type" value="application/x-www-form-urlencoded"> 
</cfhttp> 

這是錯誤:

{ "error" : "invalid_request", "error_description" : "Required parameter is missing: grant_type" } 

什麼我錯在這裏做什麼?我沒有看到問題。請幫忙。

我跟了這https://developers.google.com/identity/protocols/OAuth2WebServer

回答

4

是否有就是爲什麼你想自己打造的POST體具體原因是什麼?爲了安全起見,請嘗試以下代碼(替換代碼段中的所有代碼行):

<cfhttp method="POST" url="https://accounts.google.com/o/oauth2/token"> 
    <cfhttpparam type="formfield" name="grant_type"  value="authorization_code"> 
    <cfhttpparam type="formfield" name="redirect_uri" value="#this.redirectURI#"> 
    <cfhttpparam type="formfield" name="client_id"  value="#this.appId#"> 
    <cfhttpparam type="formfield" name="client_secret" value="#this.key#"> 
    <cfhttpparam type="formfield" name="code"   value="#code#"> 
</cfhttp> 
+0

非常感謝@亞歷山大。 –