Google Drive API v3 - OAuth2 using Service Account in PHP/JWT
- Joel Lipman
- Hits: 12227
This is an article explaining the code needed to write a PHP script which generates an access token for a service account which in turn is used to list files in a team's Google Drive.
This is very different to my code for OAuth when attended: Google Authentication - OAuth 2.0 using PHP/cURL... In that this one doesn't prompt for a user. Again this script doesn't need the client libraries, composer, vendor, etc.
Why?
This took me the best part of a month to get working. It is taken from Google's documentation as well as other forums and websites that try to explain it. Do not waste your time like I did on the public key, verifying a JWT signature, or including any third-party libraries. You can do it in pure PHP and all you need is the JSON key that you generated in your Google console.
Applies to:
- Google Drive REST API v3
- Google OAuth 2.0 v4
- Google Cloud Platform IAM
- Google Suite
- PHP v5.6.35
How?
I'm going to go through each section of the code to go through the logic and highlight any changes you may need to make.