我正在尝试使用Google+让用户登录。我已经在Google开发人员控制台创建了项目,获取了客户端ID,并在Android Studio中安装了Google Play服务。(我的运行设备是Android 4.4.2)当应用程序运行时,我按下登录按钮后会出现一个提示消息:“内部错误”。Logcat中的错误如下:
error ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{429ffe38: android.os.BinderProxy@429ffdd8}}
请注意,这个错误是在用户按下按钮之前出现的。事实上,通过调试,我发现它来自于这行代码:
Log.e(TAG, "error " + result.toString());
这段文字出现在方法中:
public void onConnectionFailed(ConnectionResult result) {
if (!mGoogleIntentInProgress) {
/* Store the ConnectionResult so that we can use it later when the user clicks on the Google+ login button */
mGoogleConnectionResult = result;
if (mGoogleLoginClicked) {
/* The user has already clicked login so we attempt to resolve all errors until the user is signed in,
* or they cancel. */
resolveSignInError();
} else {
Log.e(TAG, "error " + result.toString());
}
}
}
以下是其他重要的方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
SignInButton googleLoginButton = (SignInButton) findViewById(R.id.login_with_google);
googleLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mGoogleLoginClicked = true;
if (!mGoogleApiClient.isConnecting()) {
if (mGoogleConnectionResult != null) {
resolveSignInError();
} else if (mGoogleApiClient.isConnected()) {
getGoogleOAuthTokenAndLogin();
} else {
/* connect API now */
Log.d(TAG, "Trying to connect to Google API");
mGoogleApiClient.connect();
}
}
}
});
/* Setup the Google API object to allow Google+ logins */
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
}
private void resolveSignInError() {
if (mGoogleConnectionResult.hasResolution()) {
try {
mGoogleIntentInProgress = true;
mGoogleConnectionResult.startResolutionForResult(this, RC_GOOGLE_LOGIN);
} catch (IntentSender.SendIntentException e) {
// The intent was canceled before it was sent. Return to the default
// state and attempt to connect to get an updated ConnectionResult.
mGoogleIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_GOOGLE_LOGIN) {
/* This was a request by the Google API */
if (resultCode != RESULT_OK) {
mGoogleLoginClicked = false;
}
mGoogleIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
}
}
以下是AndroidManifest.xml文件中的代码:
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
还有元数据
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
Hope you can help me, thanks a lot!