15

In my Android application, I am trying to get google token to verify at my google app engine backend server. But I always get this exception :

com.google.android.gms.auth.GoogleAuthException: getToken(Unknown Source) exception

My code :

    private Account mAccount = AccountManager.get(this).getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE)[0];
    private static final String SCOPE = "audience:server:client_id:" +Constants.WED_CLIENT_ID;
    String token = GoogleAuthUtil.getToken(LoginActivity.this, mAccount.name, SCOPE);

I have tried every solution proposed in Stackoverflow.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
Shikha Shah
  • 753
  • 3
  • 12
  • 34

3 Answers3

16

After several hours, I found that in your scope string ("audience:server:client_id:...") you have to use the Client ID of the web application, not the android one.

The client ID of the android app is unused. It's only here to link the package name of your android app with your web app.

Dalmas
  • 26,409
  • 9
  • 67
  • 80
  • 2
    But I am using WEB CLIENT ID..I am not using Android ID anyway..Problem was something else which I have already answered..Thanks!! – Shikha Shah Feb 28 '14 at 20:19
  • hi! same problem, although not getting the solution yet. in my scope i am using the Client ID in the Web Client created in the Google Developer Console, but its not working :( – Koustuv Sinha Oct 26 '15 at 11:15
  • That worked! Changed from using the Android app Client ID to "Web client (auto created by Google Service)" and it worked! – Bobby Mar 25 '16 at 04:05
15

After 2 days of research I found that at the time you register your android application in Google Cloud Console -> your project - > API and auth, it ask you for fingerprint in order to generate ClientID and somehow my fingerprint was wrong (I accidentally added ~$ before this cmd keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1) and due to this it was giving me wrong finger print and was the root cause of:

com.google.android.gms.auth.GoogleAuthException: getToken(Unknown Source) exception.
Xavi
  • 20,111
  • 14
  • 72
  • 63
Shikha Shah
  • 753
  • 3
  • 12
  • 34
2

After reading couple of posts on the internet I generated SHA1 fingerprint value of my keystore as

keytool -exportcert -alias android_keystore -keystore android_keystore -list -v | openssl sha1

due to which I was getting that error. If you see the credentials screen in you google developer console it says you need to use

keytool -exportcert -alias android_keystore -keystore android_keystore -list -v

i.e Don't pipe the output to openssl sha1. Not sure what the difference is but both commands give different values.

You will get multiple fingerprints like MD5, SHA1, SHA256 etc. Use SHA1 value.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289