Search code examples
androidgoogle-oauthgoogle-drive-android-api

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 least from a first read) it looks like there must always be a consent screen where the user, after choosing his google account, agrees with the app getting data from his drive.

The point is that I'd like to 'hardcode' an account that programatically agrees to that since our app is always going to take the media from the same account.

Is that possible??

PD: Don't suggest any other storage service, Drive is a client's requirement.


Solution

  • 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();