25

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

FlipNovid
  • 1,191
  • 3
  • 12
  • 20
  • Please read my answer at http://stackoverflow.com/questions/34099208/google-sign-in-idtoken to see if it can help or not – BNK Jan 09 '16 at 04:37
  • Are you sure that your app uses Web-type Id, not Android-type one? – BNK Jan 11 '16 at 00:17
  • [http://i.stack.imgur.com/6keg0.png] check please @BNK – FlipNovid Jan 11 '16 at 00:19
  • 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 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 08:56

4 Answers4

52

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.

Faisal
  • 752
  • 6
  • 9
5

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

zkvarz
  • 611
  • 1
  • 8
  • 18
  • 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 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 08:56
  • @AlekseyTimoshchenko: You are getting an id_token back so it should be oauth2/v3/tokeninfo?id_token=xxx – dannyroa Dec 03 '16 at 02:33
1
public class GplusTokenUtils extends AsyncTask<Void, Void, String> {

    String accessToken = "";

    private GoogleSignInAccount account;

    public IGetToken iGetTokenListener;

    public interface IGetToken {
        void onGetToken(String token);
    }

    public void getTokenLitener(IGetToken tokenListener) {
        this.iGetTokenListener = tokenListener;
    }

    public GplusTokenUtils() {

        iGetTokenListener = new IGetToken() {

            @Override
            public void onGetToken(String token) {

            }
        };
    }


    private Activity activity;

    public void setActivity(Activity activity) {

        this.activity = activity;
    }


    public void setAccount(GoogleSignInAccount account) {

        this.account = account;
    }


    @Override
    protected String doInBackground(Void... voids) {

        try {
            accessToken = GoogleAuthUtil.getToken(activity, account.getEmail(), "oauth2:profile email");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (GoogleAuthException e) {
            e.printStackTrace();
        }

        return accessToken;
    }

    @Override
    protected void onPostExecute(String aVoid) {

        Log.d("GPLUS_TOKEN", aVoid);

        iGetTokenListener.onGetToken(aVoid);

    }

}

And don't forget this

  GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();
Caner Yılmaz
  • 201
  • 3
  • 6
0

I tried all the solutions I saw, but nothing helped. But in the end, I simply cleared the data and cache of the application, and the token arrived.