Google Drive Data Retreive with OAuth 2.0





What is OAuth?

OAuth (Open Authorization) is an open standard for token-based authentication and authorization on the Internet.

                                           

Initial Steps !!!

First, we need to create and register an app in Google Developer Console. In order to do that navigate to

Google Developer Console

Enable Google Drive and Google+ APIs

this demo we’ll be enabling Google Drive APIs and Google+ APIs. You can
search and enable these APIs and continue with the article.



  • Google Drive API
  • Google+ API

Lets navigate to the “Dashboard” tab and select “Enable APIs and Services” to select and enable all wanted Google APIs and services.

Creation of Application



Authorization Tokens

Retrieving authorization tokens happens in two folds.
  1. A request made from client application requesting an authorization code
  2. A request made from client application requesting access tokens using the authorization code




Get Code

To request permission for our created express app, we have to call the following end-point with GET parameters.

https://accounts.google.com/o/oauth2/v2/auth

GET Parameters

access_type="offline"
client_id="id"
response_type="code"
scope="URL encoded Scopes"

redirect_uri="http://localhost/ssd_sliit/assignment2/process.php"

For Scope we can define the url's which need to retrieve data


  • https://www.googleapis.com/auth/drive
  • https://www.googleapis.com/auth/drive.metadata.readonly
  • https://www.googleapis.com/auth/plus.me
The final request URL will be as following

https://accounts.google.com/o/oauth2/v2/auth?client_id=id&response_type=code&scope=https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/plus.me&redirect_uri=http://localhost/ssd_sliit/assignment2/process.php&access_type=offline



Get Access Tokens

Now, we need to do a POST request to the following url with the following body elements to request

https://www.googleapis.com/oauth2/v4/token

Header
Content-Type:application/x-www-form-urlencoded

POST Request Body

code="Retrieved code"
client_id="Your generated Client ID"
client_secret="Your client secret"
grant_type="authorization_code"
redirect_uri="http://localhost/ssd_sliit/assignment2/process.php"

A successful request will give us the following response

{
  "access_token": "some string as access token",
  "token_type": "Bearer",
  "expires_in": 3600,
  "refresh_token": "some string as refresh token"
}



You can now view the Google plus account as well as the files in the google drive and download.

Reactions

Post a Comment

0 Comments