Reputation: 819
I am trying to use Gmail API without Google Dll, and I would like to use it only with HTTP requests. How can I authenticate and authorize with scopes (like creating a service with Google dll)?
I am getting an error
Upvotes: 3
Views: 4677
Reputation: 117146
You can authenticate to Google using any language that can handle a HTTP POST and a HTTP GET.
Note: client_id, redirect_uri, client_secret are all values that you have set up for your app in Google Developers Console. Scope will depend upon which Google Api you would like to access, more then one can be separated by a comma. I will be using the scope for Google Analytics in this example.
Step one request access:
This is the URL you will need to display to the user requesting access. It is a HTTP Get call and can be placed in any web browser. Note: response_type=code
https://accounts.google.com/o/oauth2/auth?client_id={clientid}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/analytics.readonly&response_type=code
Step Two:
Once they click on the above link you should get a authentication code.
The following request will exchange the code for an access token and a refresh token. This is a HTTP POST Note: grant_type=authorization_code
https://accounts.google.com/o/oauth2/token
code=4/X9lG6uWd8-MMJPElWggHZRzyFKtp.QubAT_P-GEwePvB8fYmgkJzntDnaiAI&client_id={ClientId&client_secret={ClientSecret}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code
response:
{ "access_token" : "ya29.1.AADtN_VSBMC2Ga2lhxsTKjVQ_ROco8VbD6h01aj4PcKHLm6qvHbNtn-_BIzXMw", "token_type" : "Bearer", "expires_in" : 3600, "refresh_token" : "1/J-3zPA8XR1o_cXebV9sDKn_f5MTqaFhKFxH-3PUPiJ4" }
Using Refresh token:
The access_token you get from the above request is what you will be using to make requests to the service. After one hour your access token will have expired you will need to request a new access_token you take the refresh_token that you got above and HTTP Post it to: Note: grant_type=refresh_token
https://accounts.google.com/o/oauth2/token
client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&refresh_token=1/ffYmfI0sjR54Ft9oupubLzrJhD1hZS5tWQcyAvNECCA&grant_type=refresh_token
This is the response:
{ "access_token" : "ya29.1.AADtN_XK16As2ZHlScqOxGtntIlevNcasMSPwGiE3pe5ANZfrmJTcsI3ZtAjv4sDrPDRnQ", "token_type" : "Bearer", "expires_in" : 3600 }
My Full tutorial Google 3 legged oauth2 flow
Usage:
Any request to the Gmail api that you wish to use just tack access_token=yourtoken on the end and you should have access.
or you can set the header.
Authorization Bearer accessToken
Upvotes: 6