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 

invalid_grant

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": "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_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/xxxxxxxxx.iam.gserviceaccount.com"
}

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


$client->setAuthConfig(__DIR__.’/service.json’);

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...