2

I have been following these two tutorials: https://developers.google.com/identity/sign-in/android/sign-in https://developers.google.com/identity/sign-in/android/backend-auth

I managed to integrate google login system into my android application but when I try to get token id I am get null.

When I try to use following code:

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

I get fail on user log in. When I call:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();

I am unable to retrieve token.

I tried to merge this code to have:

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

but without any success.

Here is my code:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

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

    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        handleSignInResult(result);
    }
}
// [END onActivityResult]

// [START handleSignInResult]
private void handleSignInResult(GoogleSignInResult result) {
    Log.d(TAG, "handleSignInResult:" + result.isSuccess());
    if (result.isSuccess()) {
        // Signed in successfully, show authenticated UI.
        GoogleSignInAccount acct = result.getSignInAccount();
        mStatusTextView.setText(getString(R.string.signed_in_fmt, acct.getDisplayName()));
        updateUI(true);

        String idToken = acct.getIdToken();
        if (idToken != null) {
            Log.d("TOKEN", idToken);
        } else {
            Log.d("TOKEN", "CHUUUUUUJ WIELKI!");
        }
    } else {
        // Signed out, show unauthenticated UI.
        updateUI(false);
    }
}
Lenny
  • 887
  • 1
  • 11
  • 32
  • 1
    http://stackoverflow.com/questions/34111978/why-is-requestidtoken-returning-null – IntelliJ Amiya Feb 10 '16 at 11:49
  • 1
    Above answer is correct! Thanks! – Lenny Feb 11 '16 at 21:09
  • Glad to hear .Move ahead . – IntelliJ Amiya Feb 12 '16 at 08:36
  • 1
    Could you please help me with my issue, i spend some days trying to solve it... I use the same code, but 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:31

1 Answers1

4

Google APIs use the OAuth 2.0 protocol for authentication and authorization. Google supports common OAuth 2.0 scenarios such as those for web server, installed, and client-side applications.

To begin, obtain OAuth 2.0 client credentials from the Google Developers Console. Then your client application requests an access token from the Google Authorization Server, extracts a token from the response, and sends the token to the Google API that you want to access. For an interactive demonstration of using OAuth 2.0 with Google (including the option to use your own client credentials), experiment with the OAuth 2.0 Playground.

For details Go Through requestIdToken returning null

Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 1
    Could you please help me with my issue, i spend some days trying to solve it... I use the same code, but 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:31
  • 1
    Hi Aleksey! I would start with checking if you have correct credentails within your developer console (SHA-1 fingerprint) and double check if you put details from developer console inside your code. Read more here `http://www.androidhive.info/2014/02/android-login-with-google-plus-account-1/` and here: `https://developers.google.com/identity/sign-in/android/start-integrating` – Lenny Aug 11 '16 at 20:42