Google+ login on Android Studio - GoogleAuthException: BadUsername
After successfully logging in, I cannot get the profile information and keep getting the error in the GoogleAuthUtil.getToken () method.
Mistake:
com.google.android.gms.auth.GoogleAuthException: BadUsername
The Android Client ID is derived automatically from the combination of the Android package name and the SHA-1 fingerprint of the signing key in the developer console project. I matched the package name and even tried the entire debug.keystore file I have on my workstation. Nothing succeeded.
* The consent screen is installed (!), But not displayed.
The whole working snippet:
public class GooglePlusActivity extends Fragment implements ConnectionCallbacks, OnConnectionFailedListener {
private static final String TAG = "SignInActivity";
// magic number use to know that sign-in error
// resolution activity has completed.
private static final int REQUEST_RESOLVE_ERROR = 9000;
// core Google+ client.
private GoogleApiClient mGoogleApiClient;
// flag to stop multiple dialogues appearing for the user.
private boolean mResolvingError;
//TOKEN
protected String mToken;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "Create");
Plus.PlusOptions options = new Plus.PlusOptions.Builder()
.addActivityTypes("http://schemas.google.com/AddActivity",
"http://schemas.google.com/ReviewActivity")
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this.getActivity())
.addApi(Plus.API, options)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
// use mResolveOnFail as a flag to say whether we should trigger
// the resolution of a connectionFailed ConnectionResult.
mResolvingError = false;
Log.i(TAG, "Signing in...");
}
@Override
public void onStart() {
super.onStart();
Log.i(TAG, "Start");
// Every time we start we want to try to connect. If it
// succeeds we'll get an onConnected() callback. If it
// fails we'll get onConnectionFailed(), with a result!
if (!mResolvingError)
{
mGoogleApiClient.connect();
}
}
@Override
public void onStop() {
super.onStop();
// It can be a little costly to keep the connection open
// to Google Play Services, so each time our activity is
// stopped we should disconnect.
Log.i(TAG, "Stop");
mGoogleApiClient.disconnect();
}
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "ConnectionFailed : " + result.toString());
if (mResolvingError) {
// Already attempting to resolve an error.
return;
} else if (result.hasResolution()) {
try {
mResolvingError = true;
result.startResolutionForResult(this.getActivity(), REQUEST_RESOLVE_ERROR);
} catch (SendIntentException e) {
// There was an error with the resolution intent. Try again.
mGoogleApiClient.connect();
}
} else {
// Show dialog using GooglePlayServicesUtil.getErrorDialog()
Log.i(TAG,"Error num- " + result.getErrorCode());
mResolvingError = true;
}
}
//String scope = "oauth2:" + Scopes.PLUS_LOGIN;
//Plus.AccountApi.getAccountName(mGoogleApiClient)
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected.");
// Turn off the flag, so if the user signs out they'll have to
// tap to sign in again.
mResolvingError = false;
if(mToken == null || mToken.isEmpty())
{
// Retrieve the oAuth 2.0 access token.
final Context context = this.getActivity();
final String scope = "oauth2:"+ Scopes.PLUS_LOGIN;
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String token = null;
try {
token = GoogleAuthUtil.getToken(
context,
Plus.AccountApi.getAccountName(mGoogleApiClient),
scope);
} catch (IOException transientEx) {
// Network or server error, try later
Log.e(TAG, transientEx.toString());
} catch (UserRecoverableAuthException e) {
// Recover (with e.getIntent())
Log.e(TAG, e.toString());
Intent recover = e.getIntent();
startActivityForResult(recover, REQUEST_RESOLVE_ERROR);
} catch (GoogleAuthException authEx) {
// The call is not ever expected to succeed
// assuming you have already verified that
// Google Play services is installed.
Log.e(TAG, authEx.toString());
}
return token;
}
@Override
protected void onPostExecute(String token) {
mToken = token;
Toast.makeText(context, "GoogleToken: "+ token, Toast.LENGTH_LONG).show();
Log.v(TAG, "Access token retrieved:" + token);
}
};
task.execute();
}
}
public void onDisconnected() {
Log.i(TAG, "Disconnected.");
}
public void onActivityResult(int requestCode, int responseCode, Intent intent) {
Log.i(TAG, "ActivityResult: " + requestCode);
if (requestCode == REQUEST_RESOLVE_ERROR) {
mResolvingError = false;
if (responseCode == GooglePlusActivity.CAUSE_SERVICE_DISCONNECTED) {
// Make sure the app is not already connected or attempting to connect
if (!mGoogleApiClient.isConnecting() && !mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
}
}
}
@Override
public void onConnectionSuspended(int arg0) {
// onConnectionSuspended
Log.i(TAG, "onConnectionSuspended.");
} }
What am I doing wrong?
+3
source to share