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?
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.