Reputation: 53
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:
Write to the application's Google Drive account, without prompting the user for picking an account. Because it is guaranteed that the user's phone will and should not have application account.
Does not ask for any permissions, because user has no clue about scopes or drive account info.
Is it possible at all?
Upvotes: 2
Views: 445
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