简体   繁体   中英

Can't get token id from google in android google log in

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?

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

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