I run a local website for Algerian students to find their documents online, a major issue I ran into is that, a lot of these documents were on Google Drive already and I needed a way to download, re-upload, sorting and sharing them on drive. I also needed to add each link of these files to my database, it was more than 600 documents.

I’m a lazy person who hate doing repetitive tasks, but yeah, all of us hate doing this, it’s just boring, quite boring actually, Imagine if you had to upload images with a certain pattern in their names every hour to Drive, or having to create a new folder every 30 minutes in Drive and so on…

Introducing Google Drive API :

Google has a great API for Drive, it can be quite hard to understand how it works first, but you don’t need to know how, we’re going to use a wrapper to make things much easier: PyDrive .

Before starting to use Google Drive API or PyDrive, you need to create a project in Google Developers Console, this is where Google will link your account with the API, provide you with keys to authenticate and track your usage rate.

Signup to the developers console using your google account, in the toolbar you will find a dropbox to select a project, right now you don’t have any project so it will ask you to create one:

Give it a name, if you have an organization like your company or college select it. Don’t worry if you don’t have one, it won’t change anything for us.

Now you should be redirected to your project dashboard, if there’s an error message telling you that you don’t have any project, just go to the blue toolbar, select your project from there.

Your dashboard should be empty, you need to select the APIs you want to activate for your project, we only need the Google Drive API, but if next time you want to play with Youtube, Maps, Gmail or any other Google App you can activate their APIs the same way.

To activate Drive API, press on the burger menu in the toolbar and select ‘ APIs and Services’ then select Dashboard:

Activating APIs for Google dev console

Search for the APIs you need to activate

Just type in Drive in the search bar, and click on the link to go to the API’s page, once there, enable it :

Once you enable it, you will be taken to a new screen :

Here you will see your API calls stats, it’s time to create our API & client keys, we use those when we make API calls so it can link up the call to our account, thus control our drive from our code.

We did not write any code yet, be we need all this setup before starting, just bear with me for now, the next step should be the last one before getting our hands dirty with code.

Getting API keys:

This is quite important for any Google API you will use, for each API you will need some keys, not just for Google APIs, even for Reddit, Twitter, Facebook and several other products.

To get these keys go to Credentials (it’s on the sidebar), and press “Create credentials” button, you will have a list of 3 options:

API key : Choosing this one will only let you access your app drive acount only. Client ID: This one is the one you need in case you’re building a web app where users can link up their drive account, this lets you access other users accounts on their consent. Service account: this one is only in case you have some kind of web service running on a server and communicating with other servers.

We need the 2nd one, even if you’d like to access only your own drive account, because the first one let you access your app drive account which is different from your own account.

But before doing that, we need to give our app a name, go to the ‘OAuth consent screen’ and give it a name in the ‘Product name’ field:

Now, let’s go back to the 1st tab, and create a Client ID, you will have to choose the type of application, just choose other as this will be a script forr our personal use.

Now you will be taken back to the credentials tab, this time you have a client ID and a client secret, both of these are important to authenticate our app through Python.

Note: it’s okay to communicate the client ID, but you always need to keep the client secret key for you, because yes you guessed it: it’s secret.

Like I promised, the setup part is done, it’s time to jump to the code !

Code Baby, Code:

I know it’s been a little bit long to setup our console the right way, but this is due to the complexity and large scale of Drive and the powerfulness of its API.

Let’s get to the code now, as I wrote earlier, we won’t be using the APIs directly because this would take a lot of time and effort, instead we will use a nice wrapper in python: PyDrive