Google Drive Access

Google Drive API enables developers to integrate Google Drive functionality into applications, offering access to file storage, sharing, and synchronization capabilities. It allows users to manage files, folders, and permissions programmatically, facilitating seamless integration of cloud storage features into various software solutions. The API supports multiple programming languages and provides robust documentation, making it versatile for a wide range of applications from document management to collaborative platforms. More information can be found here.

To connect DE Africa notebooks and other files with Google Drive API using a service account, a service account is created in the Google Cloud Console. Users must then download its JSON key file, and configure permissions on both Google Drive and the service account. This approach bypasses user authentication and allows server-to-server interactions, making it suitable for automated tasks or applications where user interaction is not required. The service account JSON key is securely stored and used by Python code within Jupyter notebooks to authenticate and authorize API requests, enabling seamless integration of Google Drive functionalities directly into data science workflows without user intervention.

The tutorials below cover:

  • Setting up a Service Account

  • Enabling Google API

  • Connecting a Google Drive folder to the DE Africa sandbox

Setting up Google Service Account

A Google service account is a special type of Google account that belongs to your application or a virtual machine (VM), instead of to an individual end-user. It is typically used when your application needs to access Google Cloud services programmatically, without requiring human intervention.

Follow the steps below create one service

  1. Go to Google Cloud

  2. On the top left click on the select a project

  1. A dialogue box will open. On the top right of the dialogue box, click on NEW PROJECT.

  1. A new page will open, on the page either keep the default name or provide your own details, afterwards click on the CREATE button.

  1. A new page will open for you. In the top left, click on 3 horizontal line and choose APIs AND Service.Click on the APIs and Service choose Credentials on the left.

  1. At the Credentials page, click on Create Credentials and select Service account

  1. Fill in the details provided below, for this example, the deafricaconnection name was used, but you can use any name of your choice. Click on the create and continue

  1. The next step is to choose the role, Grant this service account access to project, under select role go to Basic and choose Onwer. After that click on continue and then click on Done buttton.

  1. The image below will show up, under the service account click on the edit button, and it will open the account created.

  1. On the detail page copy the service account email and keep it, as it will be used later on

  1. Click on the KEYS, at this section click on ADD KEY and choose JSON, click on CREATE button. A json file will be downloaded unto your machine and rename it to credentials.json

Enabling Google API

  1. Visit https://console.cloud.google.com/apis/library/drive.googleapis.com

  2. Make sure the project you created is shown in the dropdown beside Google Cloud

  3. Click on the ENABLE button to enable the Google API

Setting up Google Drive Folder

  1. Open the Google Drive, and Create a folder, give it any name of your choice.

  2. Once the folder is created, right click on the folder and click on share.

  3. Share the folder with the service account email that was copied earlier, and set permission to editor. Click on Done.

  4. Open the created folder and copy the highlighed text in the url as shown in the image below as it will be used later on.

Connecting Folder to DE Africa sandbox

  1. Log into your DE Africa Sandbox account.

  2. Locate the Supplementary_data Folder, open it and look for DriveCredentials folder.

  3. Upload the credential.json file in the DriveCredentials folder.

  4. Create a text file on your desktop and remane it to googledrive_id.txt

  5. Open the text file and paste the text copied from the Google Drive folder url earlier on.

  6. Upload the googledrive_id.txt file in the DriveCredentials folder. if you want to use another folder in google drive,update the id in the googledrive_id.txt file.

  7. After the steps above, locate the notebook Integrating_with_Google_Drive.ipynb in the Frequently_used_code Folder in the DE Africa Sandbox. The Integrating_with_Google_Drive.ipynb notebook gives step by step guide on using python functions to upload and download files from your google drive.

Now we are good to go and tryout the function.