Hello guys,

In this blog, I will discuss how to implement the google drive in Ionic 2. We will implement google drive with the help of Rest API’s. We will be uploading a file, retrieving the file and search the file in google drive.

Let’s get started, We have to install ionic ‘Google Plus’ plugin in our project with client id. This plugin allows you to authenticate and identify users with Google Sign-In. To use this plugin we have to log-in to developer console https://console.developers.google.com and create a project there and enable the Google Drive API and Google Plus API.

We have to follow these steps in order to make Google Sign-In work.
Step 1:- Go to credentials
Step 2:- Choose OAuth consent screen
Step 3:- Fill all the required details
Step 4:- Choose OAuth client id
Step 5:- Fill all the required details and For SHA1 key use this ‘keytool -exportcert -keystore ~/.android/debug.keystore -list -v’ in Linux.
Step 6:- Copy Client Id & use as reverse client id

Note:- For more detail follow this link

Now install ‘Google Plus’ plugin with REVERSED_CLIENT_ID

$ionic cordova plugin add cordova-plugin-googleplus –variable REVERSED_CLIENT_ID=com.googleusercontent.apps.177554691045-29njrg19ttgub9739rty1i5grct60p2q
$npm install –save @ionic-native/google-plus.

Note:- At the time signing with Gmail account we should send the scope to access the drive for access permission. In the following manner : –

Upload the file on google drive:-

Before uploading the file on the drive we should create the file first in our project and then insert the data into the file. To create a file in ionic, we can follow this link https://ionicframework.com/docs/native/file.

  • To upload the file on Google Drive we use following the API “https://www.googleapis.com/upload/drive/v3/files?uploadType=media”
  • In this API we send the user authentication token i.e. access token in the header section and file content should be sent in the body section.

Steps above describe uploading the data to Google Drive. We have used “https://www.googleapis.com/drive/v3/files/” Endpoint to change the name of the text file on Google Drive.

Retrieving the file from Google Drive:-

  • Before retrieving the file from the drive firstly, we have to search for the file in google drive.
  • To search the file in Google Drive we use the endpoint i.e. “‘https://www.googleapis.com/drive/v3/files”. Which retrieves all the files from your drive.
  • Now we search our file name in the file list, If it exists then we take out the fileID of that particular file which consists of our data.
  • After Searching the file in the drive we call the API which retrieves the data from the file i.e. “https://www.googleapis.com/drive/v3/files/’ + this.driveFileId + ‘?alt=media”

You can refer to the code snippet given below for more clarification.
Searching the file in the Drive:-

Retrieve the Data from the file:-

I hope this helps, if any extra information is required please comment below.

Happy Coding. 🙂