Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Token null Sign-in Google Account

I am following the example of google to get the token but without success. Always fails to acquire the token. This is latest way Google displays on your page developers I believe the error is not in my code

  private String CLIENTE_ID = "...apps.googleusercontent.com";



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

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

imgBGoogle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

            Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
            startActivityForResult(signInIntent, 9002);
        }
});



@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 == 9002) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);

            handleSignInResult(result, data);

        }

if (requestCode == 9002) {
            // [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);
                Log.d(TAG, "\n ");

                // TODO(user): send token to server and validate server-side
            } else {
                // Show signed-out UI.
                Log.d(TAG, "idToken: fail");

            }
            // [END get_id_token]
        }



}

private void handleSignInResult(GoogleSignInResult result, Intent data) {

        getToken1(data);
        getToken2(result);

        String BOOKS_API_SCOPE = "https://www.googleapis.com/auth/books";
        String GPLUS_SCOPE = "https://www.googleapis.com/auth/plus.login";
        String mScopes = "oauth2:" + BOOKS_API_SCOPE + " " + GPLUS_SCOPE;

}


void getToken1(Intent data){

    GoogleSignInResult a = Auth.GoogleSignInApi.getSignInResultFromIntent(data);

    if (a.isSuccess()) {

        Log.d(TAG, "TOKEN 1: " + a.getSignInAccount().getIdToken());
        Log.d(TAG, "DISPLAY NAME 1: " +a.getSignInAccount().getDisplayName());
        Log.d(TAG, "ID 1: " + a.getSignInAccount().getId()+"\n ");

    }else{
        Log.d(TAG, "ID 1: falhou"+"\n ");
    }

}

void getToken2(GoogleSignInResult result){

        if (result.isSuccess()) {

            GoogleSignInAccount acct = result.getSignInAccount();

            Log.d(TAG, "TOKEN 2: " + acct.getIdToken());
            Log.d(TAG, "DISPLAY NAME 2: " + acct.getDisplayName());
            Log.d(TAG, "ID 2: " + acct.getId()+"\n ");

        }else{
            Log.d(TAG, "ID 2: falhou"+"\n ");
        }

}

how can I get the token? can anyone help me?

enter image description here

enter image description here

enter image description here

enter image description here

like image 442
FlipNovid Avatar asked Jan 08 '16 20:01

FlipNovid


People also ask

What is Google OAuth token?

The id_token is used in OpenID Connect protocol, where the user is authenticated as well as authorized. (There's an important distinction between authentication and authorization.) You will get id_token and access_token. The id_token value contains the information about the user's authentication.

How can I get Google ID token in android?

Send the ID token to your server First, when the user signs in, get their ID token: When you configure Google Sign-in, call the requestIdToken method and pass it your server's web client ID. // make an additional call to personalize your application.


2 Answers

I just stumbled upon the similar issue, I wasn't using a web OAuth client, it worked using the firebase and I thought this simpler solution might be helpful for someone.

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

There is no need to define default_web_client_id, it will work as pasted. It is a string generated by google-services plugin.

like image 122
Faisal Avatar answered Oct 17 '22 09:10

Faisal


It seems your code is fine, but try to follow official guide exactly. It worked for me: http://android-developers.blogspot.com/2016/03/registering-oauth-clients-for-google.html

Make sure you use web token, try to create a new one and use it. enter image description here

like image 5
zkvarz Avatar answered Oct 17 '22 08:10

zkvarz