Android get google token id

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);
    }
}

Answers


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


Need Your Help

How to make a disabled button appear same as enabled buttons using javascript?

javascript jquery html dom

I have a button which is having a text field which is always disabled. I want that text field to appear as bright as it will appear when the button in enabled.

Jquery Browser Scrolling issues

javascript jquery height scrollbar

I want to test if a web page has a vertical scrollbar to add some padding pixel in the bottom of page for the reason to fix the position if a some box in the bottom. The problem is that when i test...