Ram
Ram

Reputation: 53

Is there a way to write to Google Drive from Android using application's account

I have a task to write files generated by an Android app to an application user account (or a service account). I have been looking around for any help and found this sample

This sample app expects that the user account is setup on the phone. It also asks for user's permission to write files to the drive account.

But given that my app uses "service account" or an application account, it should:

Is it possible at all?

Upvotes: 2

Views: 445

Answers (1)

Burcu Dogan
Burcu Dogan

Reputation: 9213

You can authorize the API with a service account without asking the user to pick an account and asking about permissions.

GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
    .setJsonFactory(JSON_FACTORY)
    .setServiceAccountId("[[INSERT SERVICE ACCOUNT EMAIL HERE]]")
    .setServiceAccountScopes(DriveScopes.DRIVE)
    .setServiceAccountPrivateKeyFromP12File(new File("key.p12"))
    .build();

Drive service = new Drive.Builder(
    new NetHttpTransport(),
    new GsonFactory(),
    credential).build();

// make requests
service.files().list().execute();

Upvotes: 2

Related Questions