How to use a Service account to access Google Drive API instead of

Per Previous post  , I had to reserve engineer a method for accessing documents in Google drive via API from WordPress , I put the access to Offline so it wouldn’t need refreshing , however after a week it suddenly stopped working with 


I put the API in testing mode which means 

Authorizations by a test user will expire seven days from the time of consent. If your OAuth client requests an offline access type and receives a refresh token, that token will also expire.

Changing this to “In production” looks like a lot of trouble 

However reading through the guides a Service account would fix this task


Create a service account and create a key for that service account

JSON key file should look like below

"type": "service_account",
"project_id": "xxxxxxxx",
"private_key_id": "xxxxxxxxx",
"private_key": "END PRIVATE KEY-----\n",
"client_email": "[email protected]",
"client_id": "xxxxxxxxxxx",
"auth_uri": "",
"token_uri": "",
"auth_provider_x509_cert_url": "",
"client_x509_cert_url": ""

Save this json as something like service.json

Share the folders in Google Drive with your service account [email protected] with the right permissions 

Remove the need for token.json


GD Star Rating
GD Star Rating

Tags: api, drive, google, invalid_grant, service account, token.json

Trackback from your site.