I'm trying to get a user token ID using the new Google play services 8.3 and as documented I pass the server ID:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
    .requestIdToken(getString(R.string.server_client_id))
    .requestEmail()
    .build();

but I'm still getting un successful result as below:

{statusCode=unknown status code: 12501, resolution=null}

and documented here GoogleSignInStatusCodes

The sign-in was cancelled by the user. i.e. the user cancelled some of the sign-in resolutions, e.g. account picking or OAuth consent.

Constant Value: 12501

That is not my case, as I already picked an account. Any idea what could be the reason?


Solution 1:

I had exactly the same problem and i have found the solution.

If you follow the documentation found here: https://developers.google.com/identity/sign-in/android/start-integrating

The first step tells you to create the configuration file (which creates an OAuth 2.0 client ID for you and inserts it into the google-services.json)

Then later, it says again about creating a OAuth 2.0 client ID, but this time it says that you have to do it for Web application

And this is the confusing part! (at least for me) because i was just taking the client id created for the android OAuth and not creating a new one for Web application (I thought the documentation was just redundant or something)

As it says, it is this one, and only this one the one you have to use as a parameter of the methods requestIdToken or requestServerAuthCode.

Forget about using the Android OAuth ID in this methods because then you will get all the time the ugly status code response 12501.

I think the main problem is that the documentation is a bit confusing about this. Or maybe because it is a bit strange the fact that you have to create two OAuth IDs.

So as a summary, you need TWO OAuth IDs, one for android and one for web application, and you have to set each one in the correct place.

Solution 2:

I was struggling with this and wasted almost a week in it.

This is how I got it worked.

  1. Import Project in AndroidStudio
  2. Create debug keystore for project.
  3. Create SHA1 signature for project using debug keystore.
  4. Using SHA1 signature, register your app for Google Signin on Google Developer Console.
  5. Generate a Google Configuration file there.(Put in Android Studio's app folder)
  6. Use Web Client ID from OAuth 2.0 credentials in your Android Project.
  7. Now, from Android Studio, generate debug build(APK) of your project.
  8. Mount the device in your system -> copy this signed debug version of APK and install it.

Last three steps 6, 7 and 8, are what you actually need to take care of. If you directly run the project then APK is not actually signed with the debug keystore and google does not recognise it at all.

Solution 3:

I had the same problem, after research solution it's resumed that server_client_id contained some incorrect value or your google_services.json didn't include oauth_client with client_id that registered with your keystore.

requestIdToken(getString(R.string.server_client_id))

R.string.server_client_id use OAuth 2.0 client ID for Web Application. And OAuth Client ID for Android use in google_services.json

Usually we use 2 keystore, 1 using debug keystore and 1 using signed keystore for published. So if we want to need in debug & publish mode, register your OAuth Client ID for Android twice, 1 using SHA1 from debug keystore and 1 from signed keystore for published.

small example in my google_services.json

  "oauth_client": [
    {
      "client_id": "xxx-client-id.com",
      "client_type": 1,
      "android_info": {
        "package_name": "com.app.android",
        "certificate_hash": "xxxhash"
      }
    },
    {
      "client_id": "yyy.client-id.com",
      "client_type": 1,
      "android_info": {
        "package_name": "com.app.android",
        "certificate_hash": "yyyhash"
      }
    }
  ],