3

Following the example from Ray Camden. All is well until I attempt to get the access token. My getAccessToken() function bombs with "Connection Failed" error. Can't figure out what I'm doing wrong?

<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())>
James A Mohler
  • 11,060
  • 15
  • 46
  • 72
bernster
  • 51
  • 3
  • 2
    Can you access `https://www.googleapis.com` from the ColdFusion server? – Miguel-F Nov 02 '18 at 17:00
  • 1
    This is more than likely related to cfhttp being used to connect via SSL (https). Have a look at this answer https://stackoverflow.com/a/20475763/8524758 for a detailed explanation and good luck! – JSONaLeo Nov 03 '18 at 15:49
  • Thank you both! The instructions at the link you provided resolved it! – bernster Nov 15 '18 at 20:29

1 Answers1

0

Replace the lines

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

with

<cfset postBody = postBody & "client_id=" & UrlEncodedFormat(application.clientid) & "&"> 
<cfset postBody = postBody & "client_secret=" & UrlEncodedFormat(application.clientsecret) & "&"> 
<cfset postBody = postBody & "redirect_uri=" & UrlEncodedFormat(application.callback) & "&"> 
BKBK
  • 484
  • 2
  • 9