Authorizing my application to use an Existing Folder

前端 未结 4 924
梦如初夏
梦如初夏 2021-01-14 00:14

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

mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Drive.API).addScope(Drive         


        
相关标签:
4条回答
  • 2021-01-14 00:57

    Google Drive Android API only support two scopes:

    drive.file: (https://www.googleapis.com/auth/drive.file) Per-file access to files created or opened by the app

    drive.appdata (https://www.googleapis.com/auth/drive.appdata) Allows access to the Application Data folder

    You need to use the Google Drive web service (https://developers.google.com/drive/v2/reference/) and your application must use the Google APIs Client Library for Java. (https://code.google.com/p/google-api-java-client)

    The scope for full access: "https://www.googleapis.com/auth/drive"

    GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE); 
    credential.setSelectedAccountName(accountName); 
    Drive service = new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build();
    
    0 讨论(0)
  • 2021-01-14 01:00
        mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Drive.API)
            .addScope(Drive.SCOPE_FILE)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    

    I think you missed the addScope, I never worked with it myself but this is what i could find missing, when looking at the documentation. documentation

    0 讨论(0)
  • 2021-01-14 01:04

    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.

    0 讨论(0)
  • 2021-01-14 01:16

    Don't use the API provided by Google Play Services. It doesn't support DRIVE scope. Without DRIVE scope, you don't have enough privilege to access files created by other apps.

    You may use DriveScopes.DRIVE, this is what I'm doing in my app. My android app needs to exchange data with my legacy desktop app.

    You need to use API from Java Jar library. https://developers.google.com/drive/web/quickstart/quickstart-java A lot of JAR files need to be included, which will increase your final APK size. But, with the help of ProGuard, the size of APK is still manageable.

    However, I foresee Google will soon enforce developers to use only Google Play Services, which is part of their business strategy. So, you need to have some sort of backup plan, in case that happens.

    For further information, please refer to https://stackoverflow.com/questions/21807602/migration-strategy-for-google-drive-android-api-from-google-api-java-client-go and Plans for drive.appdata scope in android google drive API

    0 讨论(0)
提交回复
热议问题