2

I've signed my android app as a api on google console, as well as my backend server as oauth client in google console.
I've also updated updated my gradle files and so what ever neccesary to get a token Id.
But somehow, I'm always failing to get a token from google.
I've checked my package name and ssh1 code from google console and matched it with my debug ssh1 via with command: keytool -list -v -keystore mydebudpath\debug.keystore

I'm going to submit my android code here:

 @Override
      public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    validateServerClientID();
    // [START configure_signin]
    // Request only the user's ID token, which can be used to identify the
    // user securely to your backend. This will contain the user's basic
    // profile (name, profile picture URL, etc) so you should not need to
    // make an additional call to personalize your application.
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.server_client_id))
            .requestServerAuthCode(getString(R.string.server_client_id), false)
            .requestEmail()
            .build();
    // [END configure_signin]

    // Build GoogleAPIClient with the Google Sign-In API and the above options.
    mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
            .enableAutoManage(getActivity()/* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

}

and my onActivityResult:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RC_GET_TOKEN) {
        // [START get_id_token]
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        Log.d(TAG, "onActivityResult:GET_TOKEN:success:" + result.getStatus().isSuccess());
        if (result.isSuccess()) {
            GoogleSignInAccount acct = result.getSignInAccount();
            String idToken = acct.getIdToken();

            // Show signed-in UI.
            Log.d(TAG, "idToken:" + idToken);
            updateUI(true);

            // TODO(user): send token to server and validate server-side
        } else {
            // Show signed-out UI.
            updateUI(false);
        }
        // [END get_id_token]
    }
}

the code result.getStatus().isSuccess(); always returns false
Please also note that in above codes my R.string.server_client_id is my server's client_id which I've created in google developer console as web application oauth2.
I've never used my android api_key in my android authentication code. Where Am I supposed to use it?
Which part Am I doing wrong?

Mehrdad Shokri
  • 1,974
  • 2
  • 29
  • 45
  • Could you please help me with my issue, i spend some days trying to solve it... I use the same code, but finally when i retrive tokenId from google api, i get a String tokenId = <857 chars>... ?? If i tryed to validate it https://www.googleapis.com/oauth2/v3/tokeninfo?access_token= <857 chars response> i all the get 'error_description": "Invalid Value' ... How are you make a validation of your response? Are you also get String tokenId = <857 chars>? – Sirop4ik Aug 09 '16 at 07:53
  • I have no idea what 857 chars is, but my problem was that I want using the right api key, make sure you use the right client id observed from google console. the process is straight forward and I think problem would be from api key. – Mehrdad Shokri Aug 09 '16 at 08:37
  • Ok, just say me, according to your code snippet of code, which value are you getting from this line `String idToken = acct.getIdToken();` ? – Sirop4ik Aug 09 '16 at 09:54
  • As the name suggests I just get a token, then you need a back-end server to get additional info. – Mehrdad Shokri Aug 09 '16 at 10:21

1 Answers1

6

here is my checklist it might be useful to you

  1. Make sure you generate the correct configure with your correct sha1 Google Generate Configure
  2. Make sure the google-services.json in your app folder is correct
  3. Go to Credentials page and make sure you choose the correct project, it should be same as the one you used to generate configuration files
  4. In the Credentials section make sure your OAuth 2.0 client IDs type is Web Application not Android
  5. Check if your api key in your string.xml match the one in your Google Developer Console

If all else fail i suggest you try to create a new api key based on a new project in Google Developer Console, goodluck!

Useful info: developers.google.com/identity/sign-in/android/start-integrating

Louis Muk
  • 76
  • 3
  • 3
    When writing an android application why would you want the key to be "Web" instead of "Android"? – GenuinePlaceholder Aug 05 '16 at 01:28
  • Could you please help me with my issue, i spend some days trying to solve it... I use the same code, but finally when i retrive tokenId from google api, i get a String tokenId = <857 chars>... ?? If i tryed to validate it https://www.googleapis.com/oauth2/v3/tokeninfo?access_token= <857 chars response> i all the get 'error_description": "Invalid Value' ... How are you make a validation of your response? Are you also get String tokenId = <857 chars>? – Sirop4ik Aug 09 '16 at 07:53
  • @CoffeeConverter it use to define relate of client and your backend. Server side will use this for validate user token that you send – cuasodayleo Jul 09 '17 at 05:44