Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authorizing my application to use an Existing Folder

Using the Android Drive API, when trying to connect using:

mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Drive.API).addScope(Drive.SCOPE_FILE)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).build();

getGoogleApiClient.connect();

I get the select an account screen and after selecting an account, I see this:

enter image description here

View and manage Google Drive files that you have opened or created with this app

What if I what to access files created by other apps?

Is there a way for my application to ask for authorization of a specific folder?

like image 377
user1537779 Avatar asked Dec 26 '22 09:12

user1537779


1 Answers

Google Play Services client library provides an Android developer with APIs for seamless integration with the individual Google services.

The library also let your apps offers a consistent user interface to obtain authorization from users to access these services with their credentials.

Access to Google Drive Service

Google Play services client library ships with Google Drive Android API that provides access to the Google Drive service.

However, Google Drive Android API currently offers only drive.file and drive.appfolder authorization scopes which are limited in a way if your app needs access to all the contents on the user's drive.

To provide drive scope, you will need to use REST APIs offered by Drive API Client Library for Java. This library has a dependency on Google APIs Client Library for Java that offers generated client libs for access to individual Google services using REST APIs from any application (web, installed or Android)

Download the Drive API v2 Client Library for Java. The libs folder contains all the globally-applicable dependencies (Google APIs Client Library for Java) you might need across all application types (web, installed, or Android application).

For Android, you will need the following jars (as described in drive/readme.html) to sort out the dependencies

google-api-services-drive-v2-rev143-1.19.0.jar
google-api-client-1.19.0.jar
google-api-client-android-1.19.0.jar
google-http-client-1.19.0.jar
google-http-client-android-1.19.0.jar
google-http-client-gson-1.19.0.jar
google-oauth-client-1.19.0.jar
gson-2.1.jar
jsr305-1.3.9.jar

Authorization

Before accessing Google Drive (or any other Google) service, you need to authorize your application (using OAuth 2.0)

  1. For applications using Google Play Services client library, this will be handled by GoogleApiClient.

  2. However, if the service/Google API you want to use is not included in the Google Play services library, you can connect using the appropriate REST API to manually make requests or using a client library provided by the service provider, but you must obtain an OAuth 2.0 token.

For drive scope, you need to use Google APIs Client Library for Java and the generated client libraries (drive v2, in our case). To obtain an authorization token, you can either -

  • directly use the OAuth 2.0 library from Google APIs Client Library for Java (not preferred for android)
  • or leverage the authorization portion of the Google Play services library using GoogleAuthUtil and AccountPicker. Read Authorizing with Google for REST APIs. (GoogleAuthUtil.getToken() caches and manages token expiry and refresh itself. However, in case of network errors/server load, you might need to use an exponential back-off algorithm before retrying for the token so to not flood the server with requests. Refer 3)
  • or use the GoogleAccountCredential defined in google-api-client-android-1.19.0.jar that comes with Google APIs Client Library for Java. The package offers Utilities based on Google Play services and GoogleAccountCredential is just a wrapper around GoogleAuthUtil and AccountPicker. This would allow you to use the same consistent authorization flow and the standard account picker UI that comes with Google Play services client library while delegating the token management and using an exponential back-off strategy (as noted above) to GoogleAccountCredential. Refer 1 for an example.

Note that with the last two approaches, you are using both Google Play services client libraries (for authorization) and Google APIs Client Library for Java along with Drive API v2 Client Library for Java (for access to Google Drive service with drive authentication scope).

Package references

  • Google Play Services client library
  • Drive API Client Library for Java
  • Google API Client Library for Java 1.19.0

Also checkout -

  1. http://www.techsfo.com/blog/2014/03/android-preparing-to-access-google-drive-with-an-app-part-1/
  2. http://developer.samsung.com/android/technical-docs/Using-Google-APIs
  3. Google Play services and OAuth Identity Tools

This answer also posted at my blog here.

like image 144
nightlytrails Avatar answered Jan 11 '23 04:01

nightlytrails