oAuth 2 (Google) and CFML without cfoauth

Hopefully, this will save someone a bunch of time. This is a very simple example of how to do oAuth 2 (in this case, with Google) in CFML without using ACF 11's new oauth tag:

1-time steps:

Use https://console.developers.google.com to add a new Project and then a new Client ID for native application.

Get your code by using your browser to visit:

https://accounts.google.com/o/oauth2/auth?scope=[urlencodedformat of API's scope from API's docs]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&client_id=[client ID from above]

Get your refresh_token by dumping the results of this cfhttp:

<cfhttp url="https://accounts.google.com/o/oauth2/token" method="post">
    <cfhttpparam name="code" value="[code from above]" type="formfield">
    <cfhttpparam name="client_id" value="[client ID from above]" type="formfield">
    <cfhttpparam name="client_secret" value="[client secret from above]" type="formfield">
    <cfhttpparam name="redirect_uri" value="urn:ietf:wg:oauth:2.0:oob" type="formfield">
    <cfhttpparam name="grant_type" value="authorization_code" type="formfield">
</cfhttp>

result:


    "access_token" : "[an access token]", 
    "token_type" : "Bearer", 
    "expires_in" : 3600, 
    "refresh_token" : "[your refresh token]" 
}

[end of 1-time steps]

Now that you have your refresh token (which should not change), you can use it whenever you need a new access token, as follows:

<cfhttp url="https://accounts.google.com/o/oauth2/token" method="post">
    <cfhttpparam name="client_id" value="[client ID from above]" type="formfield">
    <cfhttpparam name="client_secret" value="[client secret from above]" type="formfield">
    <cfhttpparam name="refresh_token" value="[refresh token from above]" type="formfield">
    <cfhttpparam name="grant_type" value="refresh_token" type="formfield">
</cfhttp>

...and then use it in your API calls, like this:

<cfhttp>
    <cfhttpparam name="Authorization" type="header" value="Bearer #deserializejson(cfhttp.filecontent).access_token#"/>
</cfhttp>

Enjoy!

3 responses so far ↓

seance de massage lyon - Mar 27, 2017 at 3:21 PM

Et moi je l'aime, non pas precisement par amour du plaisir, quoique peut-etre j'aie
a repondre d'un peche aussi grave; mais j'y suis conduit en partie par le besoin de nourrir ma vengeance, car je soupconne que ce More lascif s'est
glisse dans ma couche.

institut de massage lyon - May 29, 2017 at 8:11 AM

Oh my goodness! Amazing article dude! Thank you, However I
am experiencing difficulties with your RSS.

I don't understand why I can't subscribe to it. Is
there anyone else getting the same RSS issues? Anyone that knows
the solution will you kindly respond? Thanks!!

Jane carr instagram @usapost - Jul 1, 2017 at 5:23 AM

???????? ???? ?? ??? ? ??????? instagram
usapost

Leave a Comment

Leave this field empty