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
Go to Google Cloud
On the top left click on the
select a project
A dialogue box will open. On the top right of the dialogue box, click on
NEW PROJECT
.
A new page will open, on the page either keep the default name or provide your own details, afterwards click on the
CREATE
button.
A new page will open for you. In the top left, click on 3 horizontal line and choose
APIs AND Service
.Click on theAPIs and Service
chooseCredentials
on the left.
At the
Credentials
page, click onCreate Credentials
and selectService account
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 thecreate and continue
The next step is to choose the role,
Grant this service account access to project
, under select role go toBasic
and chooseOnwer
. After that click oncontinue
and then click onDone
buttton.
The image below will show up, under the service account click on the edit button, and it will open the account created.
On the detail page
copy the service account email
and keep it, as it will be used later on
Click on the
KEYS
, at this section click onADD KEY
and chooseJSON
, click onCREATE
button. A json file will be downloaded unto your machine and rename it tocredentials.json
Enabling Google API
Visit https://console.cloud.google.com/apis/library/drive.googleapis.com
Make sure the project you created is shown in the dropdown beside Google Cloud
Click on the
ENABLE
button to enable the Google API
Setting up Google Drive Folder
Open the Google Drive, and
Create
a folder, give it any name of your choice.Once the folder is created, right click on the folder and click on share.
Share the folder with the service account email that was copied earlier, and set permission to
editor
. Click onDone
.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
Log into your DE Africa Sandbox account.
Locate the
Supplementary_data
Folder, open it and look forDriveCredentials
folder.Upload the
credential.json
file in theDriveCredentials
folder.Create a text file on your desktop and remane it to
googledrive_id.txt
Open the text file and paste the text copied from the Google Drive folder url earlier on.
Upload the
googledrive_id.txt
file in theDriveCredentials
folder. if you want to use another folder in google drive,update the id in thegoogledrive_id.txt
file.After the steps above, locate the notebook
Integrating_with_Google_Drive.ipynb
in theFrequently_used_code
Folder in the DE Africa Sandbox. TheIntegrating_with_Google_Drive.ipynb
notebook gives step by step guide on using python functions to upload and download files from your google drive.