1

I set up the authorization process for Google Play Services as described under https://developers.google.com/drive/android/auth

Now that the user has authorized the app I want to retrieve the account name. But I can't find any method in the API (http://developer.android.com/reference/gms-packages.html) that would be helpful.

WolfgangM
  • 243
  • 2
  • 14

3 Answers3

2

I assume that you already have QUICKSTART or DEMO, or something similar up-and-running, so I will refer to these 2 examples. In the BaseDemoActivity.java code, you'll notice that account selection is invoked when connection fails,

@Override public void onConnectionFailed(ConnectionResult result) {
  ...
  result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
  ...
}

... and it comes back in onActivityResult(). I just grab the intent data and get the KEY_ACCOUNT_NAME, it is the selected email. The code below is modified from the DEMO's BaseDemoActivity.java (I mentioned above).

@Override protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
  switch (requestCode) {
    case REQUEST_CODE_RESOLUTION:
    if ((resultCode == RESULT_OK) && (data != null) && (data.getExtras() != null ))
      // user selected account, get it
      String email = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
    else
      finish();    // user cancelled selection, an easy solution
    break;
  }
Nimantha
  • 6,405
  • 6
  • 28
  • 69
seanpj
  • 6,735
  • 2
  • 33
  • 54
  • @lebenlechzer Another issue, #21610239 deals with this as well. If you follow it all the way to Github, there is some code I used to handle (maybe unrelated) problem. – seanpj Feb 08 '14 at 17:55
  • 1
    Hi I am doing same thing but i am getting data null have any idea – Vishal Mokal Dec 04 '15 at 09:25
  • Have no clue. Still, I would go and check the status of GooPlaySvcs on your device. Version mismatch? Clear cache? Zap GooPlaySvcs data (dangerous for other apps; I have a dedicated dev device that I can mess up). – seanpj Dec 04 '15 at 13:10
  • 2
    Same problem, the picker comes up fine, and an account is selected (and I even store data there successfully) but the Intent data for this call back is null. If I instead use "AccountPicker.newChooseAccountIntent" I do get the account name... – john16384 Feb 13 '16 at 23:09
  • Is there any solution for the issue? it comes empty for me as well. – Lucas Eduardo Aug 12 '16 at 02:05
1
  1. Check if user signed or not

    GoogleSignInClient mGoogleSignInClient;
        private void setupAuthorization() {
            GoogleSignInOptions signInOptions =
                    new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                            .requestEmail() // add it if you want to user email 
                            .requestScopes(Drive.SCOPE_FILE)
                            .build();
            mGoogleSignInClient = GoogleSignIn.getClient(this, signInOptions);
            mGoogleSignInClient.silentSignIn().addOnSuccessListener(googleSignInAccount -> {googleSignInAccount.getEmail()}).addOnFailureListener(e -> {});
    }
    
  2. If you want to get email and name after signing in

    @Override protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case REQUEST_CODE_DRIVE_SIGN_IN: Log.i(TAG, "Sign in request code"); // Called after user is signed in. if (resultCode == RESULT_OK) { Log.i(TAG, "Signed in successfully."); // Use the last signed in account here since it already have a Drive scope. GoogleSignInAccount googleSignInAccount = GoogleSignIn.getLastSignedInAccount(this); googleSignInAccount.getEmail(); mDriveClient = Drive.getDriveClient(this, GoogleSignIn.getLastSignedInAccount(this)); // Build a drive resource client. mDriveResourceClient = Drive.getDriveResourceClient(this, GoogleSignIn.getLastSignedInAccount(this)); } break;}}

The details could be found here

thanhbinh84
  • 17,876
  • 6
  • 62
  • 69
0

Looks like data Intent is always null :(

public void startResolutionForResult(Activity var1, int var2) throws SendIntentException {
    if(this.hasResolution()) {
        var1.startIntentSenderForResult(this.mPendingIntent.getIntentSender(), var2, (Intent)null, 0, 0, 0);
    }
}

This code is from ConnectionResult.class