Enabling Google Drive API for your account and getting OAuth 2.0 authentication for it.














































Enabling Google Drive API for your account and getting OAuth 2.0 authentication for it.



Google has an API for Drive but, it's quite hard to understand how it works. However, "you don't need to know how?", as we're going to use a wrapper (It means a library with a set of enhanced function to handle API calls and make it easier to use through a given language) which makes things much easier, it's called PyDrive.
But, 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.

Steps:
1)    Signup to the Google 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:  
2)    Search for "Google Drive API" in the search box present on the top mid of the Developers Console dashboard webpage (as shown in first image below). Which will lead you to a new page (as shown I second image below):

Once you Enable the API you will be redirected to a new page, on this page click on to the "Create credentials" button given:
 
3)    Getting API Keys:
a)    API key: Choosing this one will only let you access your app drive account only.
b)    Client ID: This one is the one you need in case you%u2019re building a web app where users can link up their drive account, this lets you access other users accounts on    their consent.
c)    Service account: this one is only in case you have some kind of web service running on a server and communicating with other servers.
Here, we require the second one of the enlisted options above. But, before that you need to go to the "OAuth consent screen" (shown below). You only need to fill the "Application name" and "Support email" boxes, then click on to the save button at the bottom of the page:  
4)    Now go back to the create credentials page and choose to create client ID and for the "type of application" option choose for "others", as our script will only be for our personal use. Now you will be taken back to the credentials tab, Click on 'Download JSON' on the right side of Client ID to download "client_secret_<really long ID>.json". The downloaded file has all authentication information of your application. Rename the file to "client_secrets.json" and place it in your working directory.

5)    Create quickstart.py file and copy and paste the following code:

from pydrive.auth import GoogleAuth
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
# Creates local webserver and auto handles authentication.

Run this code with python quickstart.py and you will see a web browser asking you for authentication. Click Accept and Voila! Finally, you are done with authentication.


Comments