简体   繁体   中英

Not able to get Auth Token using GoogleAuthUtil.getToken

I am trying to get Auth Token on Android which can be exchanged for accessToken and RefreshToken on my webserver. I am following this documentation .
Specifically the section - Android app obtains offline access for web back-end

My code:

String WEB_APP_CLIENT_ID = "***.apps.googleusercontent.com";
String scopes = "oauth2:server:client_id:" + WEB_APP_CLIENT_ID+":api_scope:" + Scopes.PLUS_LOGIN + " https://www.googleapis.com/auth/plus.profile.emails.read";
//String this_scope_works_but_gives_access_token = "oauth2:" + Scopes.PLUS_LOGIN + " https://www.googleapis.com/auth/plus.profile.emails.read";
try {
    String authToken = GoogleAuthUtil.getToken(AttachEmailActivity.this, account,scopes);
} catch (GoogleAuthException e) {
    e.printStackTrace();
}

But it's always giving me following exception:

com.google.android.gms.auth.GoogleAuthException: Unknown
com.google.android.gms.auth.GoogleAuthUtil.zza(Unknown Source)
com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
app.activities.AttachEmailActivity$1.run(AttachEmailActivity.java:437)

I have read and tried out almost all solutions on similar problems it is still failing. I have tried using all different client IDs - including web client id, server application id, android client id, other client id

In fact, I used rest API outside my JAVA application using the same web client ID and I was able to get the required auth code.

https://accounts.google.com/o/oauth2/auth?response_type=code&client_id= ****.apps.googleusercontent.com&redirect_uri= http://localhost&scope=https://www.googleapis.com/auth/plus.login&access_type=offline

But in Java application, I am only getting the "Unknown" exception. Can't google give more information? What's unknown? Is there a way to get the com.google.android.gms.auth package code and see where the exception is being thrown from?

这是凭据仪表板的视图

In my case I need to catch UserRecoverableAuthException to ask for user permission

String scopes = "oauth2:server:client_id:"
        +
        getResources().getString(R.string.google_auth_client_id)
        +
        ":api_scope:" + Scopes.PLUS_LOGIN;
try {
    String token = GoogleAuthUtil.getToken(MyActivity.this,
            Plus.AccountApi.getAccountName(googleApiClient), scopes);
    subscriber.onNext(token);
} catch (UserRecoverableAuthException e) {
    startActivityForResult(e.getIntent(), 888);
    e.printStackTrace();
} catch (GoogleAuthException | IOException e) {
    e.printStackTrace();
}

and then get token inside onActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 888) {
        if (data != null && resultCode == Activity.RESULT_OK) {
            Bundle extra = data.getExtras();
            String token = extra.getString(getResources()
                    .getString("authtoken"));
            // Do something with your token
        }

        if (!googleApiClient.isConnecting()) {
            googleApiClient.connect();
        }
    }
}

Check the following below

1.Check the registered Key is work or not ?

2.Check your key for Debug or Signatured apk.

3.If the key for Signatured apk, It doesnt works for debug mode.

4.so you should create signatured apk and install in your android mobile and check it works or not?

5.Are you got a popoup (like below) when you login through Gmail profile in your Android App.?

6.if you doesn't get the popup(consent screen) when choosing the gmail account. surely the problem is your registered key

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM