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


Format of this file is like below


How do get this 

  1. Create a new google account for Google drive and enable that account for API access ( )
  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



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


The script used the existing code here

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

It will come back with an access_token and refresh_token

create a Json file with this in per below 


How to Create Permissions so files are public

Folder or File ID in fileId

Request Body

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

GD Star Rating
GD Star Rating
Google Drive API \ Credential.json and token.json ( Access Token ), 2.0 out of 10 based on 1 rating

Tags: access token, access_token, api, credentials.json, curl, Google Drive, google drive api access token without cli, PHP, STDIN, token.json, Use of undefined constant STDIN

Trackback from your site.