Skip to content Skip to sidebar Skip to footer

Android Google Drive Rest API: Skip Consent Screen (hardcoded Account)

I was trying to use Google Drive to sync some media files (video/images) and show them in my android app. I've managed to make it all work fine but according to the docs (or at lea

Solution 1:

To avoid the user consent, you can use a Service Account.

It's stated in the link that, Google APIs such as the Prediction API and Google Cloud Storage can act on behalf of your application without accessing user information. In these situations your application needs to prove its own identity to the API, but no user consent is necessary. Similarly, in enterprise scenarios, your application can request delegated access to some resources.

For these types of server-to-server interactions you need a service account, which is an account that belongs to your application instead of to an individual end-user. Your application calls Google APIs on behalf of the service account, and user consent is not required. (In non-service-account scenarios, your application calls Google APIs on behalf of end-users, and user consent is sometimes required.)

You can also check this related SO questions for more infor mation.


Solution 2:

Finally I've managed to make it work altogether:

  1. Create a service account credential from your google developers console.
  2. Download your credential in json format.
  3. Copy the file into assets/res/raw.
  4. Rename your json into something without weirds chars (Ex: driveserviceprivatekey.json).
  5. List item

        InputStream privateJsonStream = dashboardActivity.getResources().openRawResource(R.raw.driveserviceprivatekey);
        GoogleCredential serviceCredential = GoogleCredential.fromStream(privateJsonStream).createScoped(Arrays.asList(SCOPES));
    
  6. Now you can use your drive service as follows:

                HttpTransport transport = AndroidHttp.newCompatibleTransport();
                JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
                this.mService = new com.google.api.services.drive.Drive.Builder(
            transport, jsonFactory, null)
            .setHttpRequestInitializer(credential)
            .build();
    

Post a Comment for "Android Google Drive Rest API: Skip Consent Screen (hardcoded Account)"