Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: GoogleAuthUtil.getToken - where is the account object supposed to come from?

GoogleAuthUtil.getToken requires for it's second parameter an account object, but when you connect with Google SignIn what you get back in the result is a GoogleSignInAccount - which isn't the same thing. Is there a way to convert the GoogleSignInAccount to an Account object?

 private void handleSignInResult(GoogleSignInResult result) {

        if (result.isSuccess()) {

            googleSignInAccount = result.getSignInAccount();

        }
    }

then later:

authToken = GoogleAuthUtil.getToken(context, [need an account here], scope);

I know that I can get the email address back by displaying the accountpicker, and I can also get the email address from the google signin result - but I can't see a way to get the entire account object.

like image 825
Jon Avatar asked Dec 27 '15 12:12

Jon


1 Answers

Using the documentation here you can see that the response has KEY_ACCOUNT_NAME and KEY_ACCOUNT_TYPE. Therefore you can create your own Account object

Code:

  if (requestCode == REQUEST_CODE_PICK_ACCOUNT) {
            // Receiving a result from the AccountPicker
            if (resultCode == RESULT_OK) {
                mEmail = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
                mType = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
                // With the account name acquired, go get the auth token
                Account account = new Account(mEmail, mType);
                String token =  GoogleAuthUtil.getToken(context, account, mScope);
            } 
like image 171
easycheese Avatar answered Oct 31 '22 14:10

easycheese