Google Drive API \ Credential.json and token.json ( Access Token )

Recently had to reverse engineer a Google Drive PHP API Script, and I thought I would document the process!

Before you go down the below path , you will need to make sure you can push your project In Production (Very Long winded )

Testing mode for your project (even in Offline mode ) the below Refresh and Access Tokens with Expire every 7 days , if not use service account

Credentials.JSON

Format of this file is like below

{"web":{"client_id":"xxxxxxxxxxxx","project_id":"xxxxxxxxxxx","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"xxxxxxxxxxxxxxxx","redirect_uris":["xxxxxxxxxxxxxx","xxxxxxxxxx"]}}

How do get this 

  1. Create a new google account for Google drive and enable that account for API access ( https://developers.google.com/drive/api/v3/enable-drive-api )
  2. Create OAuth client ID and Select Web Application , add the authorize URLs and create 
  3. Download Crdential.json of the client your just created

 

Token.JSON

This is what they can your Access Token , Format of this file is like below

{"access_token":"xxxxxxxxxxxxxxxxxx","expires_in":3599,"scope":"https:\/\/www.googleapis.com\/auth\/drive.metadata.readonly","token_type":"Bearer","created":1625147606,"refresh_token":"xxxxxxxxxxxxxxxxxxxxxx"}

The script used the existing code here https://developers.google.com/docs/api/quickstart/php

You can see that the script is supposed to retrieve the token.json file when you give it the crdential file, however when I ran this I got 

Notice: Use of undefined constant STDIN - assumed 'STDIN'

Warning: Use of undefined constant STDIN – assumed ‘STDIN’ (this will throw an Error in a future version of PHP) in 

Warning: fgets() expects parameter 1 to be resource, string given in on line 3

Further research shows “only the CLI (command line) SAPI defines I/O constants such as STDINSTDOUT, and STDERR

I didn’t have access to PHP Cli Easily , so I performed the below ( this was a big help but it pulled an Application Access Token instead of Web

To get this you have to post stuff to google api and get a file

  1. Open Up your Credential.json file and get your Client_id and Client Secret
  2. Change the below to have your Client ID and correct redirect_uri and scope 
    https://accounts.google.com/o/oauth2/auth?client_id=XXXXXXXXXXX&redirect_uri=XXXXXXXXX&scope=https://www.googleapis.com/auth/drive.metadata.readonly&response_type=code&access_type=offline&prompt=consent
  3. Post this into a browser that’s logged into the google account where you API is registered Go through the prompts to allow access , after this is done it will take you back to your redirect_uri and give you an Authorisation code per below

  4. Format https://redirect_uri/?code=[Authorisaton Code]&scope=[SCOPE] ( Copy and save the Authorisation Code ) 
  5. Next you will need a Linux machine with Curl , you can install Linux on Windows using WSL
  6. Open up the Linux terminal and paste the below

    curl \

–request POST \
–data “code=[Authorisaton Code]&client_id=[CLIENT ID]&client_secret=]CLIENT SECRET]&redirect_uri=[REDIRECT URI]&grant_type=authorization_code” \
https://accounts.google.com/o/oauth2/token

It will come back with an access_token and refresh_token

create a Json file with this in per below 

{“access_token”:”xxxxxxxxxxxxxxxxxx”,”expires_in”:3599,”scope”:”https:\/\/www.googleapis.com\/auth\/drive.metadata.readonly”,”token_type”:”Bearer”,”created”:1625147606,”refresh_token”:”xxxxxxxxxxxxxxxxxxxxxx”}

How to Create Permissions so files are public

Folder or File ID in fileId

Request Body

{
“role”: “reader”,
“type”: “user”
}

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 2.00 out of 5)
Loading...