0

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?

Here is the view of credentials dashboard

2 Answers2

0

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();
        }
    }
}
Niko Adrianus Yuwono
  • 11,012
  • 8
  • 42
  • 64
  • Code looks similar but for me the issue comes in getToken call itself. If I use the simpler scope string (i.e. without server:client_id") it shows the authentication string and returns the accessToken. – Sumit Nagpal Nov 05 '15 at 05:47
0

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

enter image description here

Venkatesh Selvam
  • 1,382
  • 2
  • 15
  • 28
  • I am getting the consent screen with the simpler scope string: "oauth2:" + Scopes.PLUS_LOGIN + " https://www.googleapis.com/auth/plus.profile.emails.read"; but with the other one, I am getting the exception. And the same client_id I am able to use in the browser using the rest API – Sumit Nagpal Nov 05 '15 at 05:49
  • I am using signatured apk and testing on my device. Perhaps I need to start afresh. :( – Sumit Nagpal Nov 05 '15 at 05:51
  • Are you got Auth Token? and Check also your android mobile Internet Connection. – Venkatesh Selvam Nov 05 '15 at 05:55
  • As I said, I am getting accessToken with the simpler scope string. There is no internet issue. – Sumit Nagpal Nov 05 '15 at 07:07