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.
The following two tabs change content below.
Eugen Mihailescu
Founder/programmer/one-man-show at Cubique Software
Always looking to learn more about *nix world, about the fundamental concepts of math, physics, electronics. I am also passionate about programming, database and systems administration. 16+ yrs experience in software development, designing enterprise systems, IT support and troubleshooting.
Latest posts by Eugen Mihailescu (see all)
- Dual monitor setup in Xfce - January 9, 2019
- Gentoo AMD Ryzen stabilizator - April 29, 2018
- Symfony Compile Error Failed opening required Proxies - January 22, 2018
Hi
Just try your script and work, but the problem, all file in google drive named untitled
any way to fix this
thanks
andreas
hi
'm getting below Error
Please help me to rectify.
Fatal error: Uncaught exception 'Exception' with message 'DateInterval::__construct(): Unknown or bad format (PTS)'
Exception: DateInterval::__construct(): Unknown or bad format (PTS) in C:\wamp\www\google-api\launch.php on line 103
Hi i need to upload file on particular folder of drive. can you help me please for that?
Use the uploadFile function and specify the filename in the format folder_id/filename where folder_id is the Google Drive Folder ID for the folder where you want to upload (eg. 0B95k2kr1bG9fODZDY0VFMGNTWTA) and the filename is your filename (eg. sample_file.txt).
How do you find out what is the Google Drive Folder ID for a specific folder? Open your Google Drive in your web browser, navigate into that folder. The address bar could look something like this:
The current folder ID is the unique identifier that follows the /folder/, ie. {your-folder-id}.
Where can I specify the file name in your code above?
Currently when uploading files the are just named 'untitled' in Google Drive.
Error: invalid_request
Invalid parameter value for redirect_uri: Uri must consist of printable ASCII characters: http://localhost/drive upload test/finale.php
any possibility to fix this
The information you provided are very generic, it's hard to understand what you did and what answer you've got. Please address any GoogleAPI question to Google Developer Community forum.