1

here's yet another pit a fell into when trying to implement the new Google Drive Android API (GDAA):
Activity A of my app needs functionality not supported by GDAA, so I resort to using the original Google Drive SDK v2 per the statement here.:

In some cases, applications may need to use the Google Drive web service to access additional features or access broader scopes than what is available in the Google Drive Android API. In these cases, your application must use the Google APIs Client Library for Java.

The auth sequence is standard:

...
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE_FILE);
credential.setSelectedAccountName(accountName);
...

The authentication / authorization with DRIVE_FILE scope is succesful.

In Activity B, I use the GDAA, going through the standard sequence

  1. x = new GoogleApiClient.Builder(this)....build();
  2. x.connect()
  3. onConnectionFailed()
  4. startResolutionForResult()

and this is where the problem appears. The user already went through account selection and authorization in Activity A, but GDAA does not know anything about it. This causes a new account selection dialog to pop up again. So the question is:
Is there a way to pass the initialized credentials to GDAA without bugging the user again (scope's the same)?
If the app needs to use both Google Drive SDK v2 and GDAA (as will probably be the case for any, but most trivial app), showing the account selection dialog more than once will certainly be a showstopper.

Androiderson
  • 16,865
  • 6
  • 62
  • 72
seanpj
  • 6,735
  • 2
  • 33
  • 54

1 Answers1

4

When building a new GoogleApiClient, you can explicitly set an account name.

x = new GoogleApiClient.Builder(this)
       .addScope(Drive.SCOPE_FILE)
       .setAccountName(accountName)...

Since the application is authorized for the account, it will automatically connect.

Burcu Dogan
  • 9,153
  • 4
  • 34
  • 34
  • Thank you. That was quick. Incidentally, your answer came as I completed implementation the other way around, i.e. 1/ starting with GDAA authorization 2/ pulling email from authorization result 3/using it in SDK v2 But this solution allows to keep my original flow. – seanpj Feb 06 '14 at 01:13
  • Your answer made me think about this: In the DriveSDKv2 I had both GoogleAccountCredential setter (setSelectedAccountName) and getter (getSelectedAccountName) available. With GDAA I use a hack like this: http://stackoverflow.com/questions/21501829/retrieve-account-name-with-the-new-google-drive-api/21517450#21517450 And keep the 'email' around. Did I put my foot in mouth by answering it without knowing about a getter method in GDAA? – seanpj Feb 06 '14 at 13:18
  • There is no getter method in GDAA, but we can quickly fix this on the next releases. Thanks for the feedback. – Burcu Dogan Feb 07 '14 at 18:28
  • Thank you, I added a consolidation answer in another (essentially the same) topic, http://stackoverflow.com/questions/21610239/how-do-i-switch-accounts-under-the-new-google-drive-android-api/21641459#21641459 cleaning the mess I made. And the 'getter' isn't really needed. – seanpj Feb 08 '14 at 03:27