Search code examples
coldfusionoauth-2.0coldfusion-10google-signin

Google Login Integration with ColdFusion


I am trying to integrate ColdFusion with Google+. I am having a problem with the grantType. First I authenticated a user using Google. Then when I am trying to get the token, its throwing an error. Below is my code:

<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>

This is the error:

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

What am I doing wrong here? I don't see the problem. Please help.

I have followed this https://developers.google.com/identity/protocols/OAuth2WebServer


Solution

  • Is there s specific reason why you want to build the POST body by yourself? Just to be on the safe side, try this (replace all lines of your code snippet):

    <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>