Integrate Google Drive REST API on Android App

Ammarptn
Ammarptn
Published in
6 min readDec 18, 2018

Google Drive API for Android is deprecated

Google just announced that they already depreciated the Google Drive API for Android. If your apps use this APIs you may get the email about this.

https://developers.google.com/drive/android/deprecation

However, the suggestion from them is moving to the G drive Rest API instead.

Moving to Google Drive Rest API

In the Rest API documents, you will found examples in JAVA but it seems not quite straightforward to integrate with Android.

The first thing is the authorization process.

Previously on Android, you can request the login dialog attached with G Drive permission and then you can start to access file and folder in the G Drive.

The example of Java Rest API requires a CREDENTIALS FILE in order to access Drive API. I found my self a little bit lost when I try to implement this but luckily Google had provided a document about Authorizing and using Rest APIs with Android.

Everything looks easier now.

Google also provides an example of Android project about how to use Google drive Rest APIs at the link below.

Now migration from old Google drive APIs for Android to Google drive Rest API should be easy for you.

I also summarized step by step in case you still stuck in some process.

Google Drive Rest Integration step by step

Step 1:

Enable Google drive API in API console right here: https://console.developers.google.com/projectselector/apis/dashboard

Step 2:

Authorize your android app. Add OAuth client ID

This step will allow you to use a login dialog in your android app. If you are migrating from old APIs. This should not be the problem.

Step 2.1 (Special Thank you MG Developer for point out this additional step, original post is here : https://medium.com/@m.godse/its-a-great-article-and-helped-very-well-301d52cba692)

2.1.1 On Google API Credential Console Click “Create OAuth client ID”

2.1.2. Select Application type as “Android”

2.1.3. Give it a friendly “Name” for the client ID, ideally APPNAME-USER-DEBUG or APPNAME-PROJECT-PROD.

2.1.4. Enter the package name, you can locate package name for your Android APP in the AndroidManifest.xml under your project.

2.1.5. SHA1 fingerprint: Here little explanation is required. Each Android app (apk) is signed using keystore. Yes even your debug apks have keystore that you may not even know. You will need to locate your default keystore on your computer.

You can also have a common keystore file for your project, if you have multiple developers. However in that case your IDE or if you are building APK headless you will need to specify the keystore location and the password.

Otherwise default “debug.keystore is located in the user/username/.android folder”. You would need keytool to read the fingerprint. Keytool is a JDK utility shipped with Android studio, locate it in your Android Studio installation “Android\Android Studio\jre\bin”

Then execute `keytool -keystore user\username\.android\debug.keystore -list -v`

Password for the default Android debug.keystore is, yes you guessed it right. It is “android”, without the quotes. This will print the keystore details which will include SHA1 fingerprint.

6. Copy and the SHA1 finger print in the SHA1 textbox.

7. Save and now you should see Google Signin Screen and GoogleSignIn.getLastSignedInAccount(context) should return the account handle.

(Thank you MG Developer again for this part)

Step 3:

add OAuth consent

open OAuth consent screen
Add scope
Select “../auth/drive”, lock icon mean you have to submit for verification.

Bear in mind that you required to submit for verification before your application goes live.

Without verification, you still can access google drive but it will only appear folder and file that your app created.

Don’t be surprised when you try to search some existed folder or file but it not return you anything.

Step 4:

Now you can start to put some code on Android

Start by add dependencies on Gradle.build

Step 5:

Google’s example already provided a JAVA helper file here. Copy and paste in your project. Trust in their magic and save your time.

Step 6:

Start Google sign in and then create a credential and provide that instance to DriveServiceHelpper

For full example : https://github.com/ammarptn/GDrive-Rest-Android/blob/master/app/src/main/java/com/ammarptn/gdriverest/MainActivity.java

Step 7:

Everything is ready. you can start to access your file on G Drive.

In order to handle the response data easier, I create a holder object to keep the result.

For example :

Next is edit the mDriveServiceHelper file by adding these code below.

G drive Mime Type

Search file

you can view more info about how to write a query and return fields at

Search folder

View files in folder

Create a text file

Create folder

Upload file

Download file

Delete file/folder

How to manage my file then?

If you did not submit for verification yet. Your app will limit to files that its created. It may hard for you to do some testing like try to upload, read a file from the Gdrive. (your app will not able to see any file that you upload at Google drive’s Web interface/app. you have to code an upload function and execute from your app)

To make our developers' life easier, I created a small file explorer library that will help you manage your files and allow your app to access any file that you want.

To use this library, just put this into your app gradle.

And open Google drive explorer by open this Activity class

This will allow you to upload any file from your phone, view and delete it. So you can test your app more efficient and spend less time

Remark

  • File instance for Google drive is not the same as File that you use on local storage.
  • When you create a folder or file on google drive, it will not replace the existing one. It is not the same behavior as java.io.file that you can replace it when writing the file with an existing name. Otherwise, you may found that your app consumes all of the user’s G drive.
  • My current solution for replacing is searching for file/folder ID and delete it before performing a file upload.
  • App data folder will remove from G drive soon. So change your mind if you have the plan to use it.
https://developers.google.com/drive/android/deprecation

Any suggestion or improvement, let me know in the comment below.

ref.

--

--