My *nix world

Upload file to Google Drive programmatically

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:

Upload file to Google Drive programmatically

Source: https://developers.google.com

 

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.
Upload file to Google Drive programmatically

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.
Upload file to Google Drive programmatically

Latest posts by Eugen Mihailescu (see all)

Tagged on: , ,

7 thoughts on “Upload file to Google Drive programmatically

  1. Jaspreet Kaur

    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

    1. Eugen Mihailescu Post author

      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:

      • ://drive.google.com/drive/u/0/folders/{your-folder-id}

      The current folder ID is the unique identifier that follows the /folder/, ie. {your-folder-id}.

  2. Nathan

    Where can I specify the file name in your code above?

    Currently when uploading files the are just named 'untitled' in Google Drive.

    1. Eugen Mihailescu Post author

      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.

Leave a Reply

Your email address will not be published. Required fields are marked *