Nowadays I am writing a WordPress plugin that involves accessing and upload file to google drive programmatically.
This process implies the following steps:
- request an authorization code from the Google Authorization Server
- get the authorization code and then use it to send a POST request in order to obtain an access token from the Google Authorization Server
- use the access token and upload the file
The authorization sequence looks like this:
Of course, you can do all of these much simple by using the Google Client Library but I don't like this approach because it implies an external dependency and perhaps a complex chain of classes that are not really necessary (just convince yourself by reading my PHP example below).
Below you can find a simplified approach of the above idea. It is a PHP code that encompasses the following blocks:
- a function that launches the authentication process
- a function that requests the authorization code
- a function that requests the access token
- a function that stores and/or load the access token to/from a local file
- a function that returns either the access token or, in case it is expired, the refresh token
- a function that uploads a file to the Google Drive associated to that access token
- Read more about Google Drive Web APIs here.
- Read more about Authorizing Your App with Google Drive here.
- Read more about Using OAuth 2.0 to Access Google APIs here.
- Read more about API for Upload Files to Google Drive here.
Now, if you think that this article was interesting don't forget to rate it. It shows me that you care and thus I will continue write about these things.