登录成功后,我无法检索任何个人资料信息,并且在 GoogleAuthUtil.getToken() 方法上不断收到错误消息。
错误:
com.google.android.gms.auth.GoogleAuthException: BadUsername
Android 客户端 ID 是根据 Android 包名称和开发人员控制台项目中签名 key 的 SHA-1 指纹的组合自动推断出来的。我匹配了包名,甚至尝试了我工作站上的所有 debug.keystore。没有任何效果。
*同意屏幕已设置 (!) 但未出现。
整个工作 fragment :
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.");
} }
我做错了什么?
最佳答案
你能发布 Plus.AccountApi.getAccountName(mGoogleApiClient)
的返回值吗?
还要确保你的 list 中有这个:
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
关于android - Android Studio 上的 Google+ 登录 - GoogleAuthException : BadUsername,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27505805/