Android版Google+帐户登录不起作用 - 状态code = SIGN_IN_REQUIRED [英] Android Google+ login doesn't work - statusCode=SIGN_IN_REQUIRED
问题描述
我试图让Google+的用户登录。我已经创建了谷歌开发者控制台项目中,我得到了客户ID和我已经安装了Android Studio中的谷歌播放服务。 (我跑步设备具有的是Android 4.4.2)
当应用程序正在运行,并且我preSS登录按钮会出现一个敬酒的消息:
内部错误
I'm trying to let the user login with Google+. I have created the project in the Google developer console, I got the Client ID and I have installed the Google play services in Android Studio. (My running device has Android 4.4.2) When the app is running and I press the login button a toast message appears: "Internal Error"
在logcat的错误是:
The error in the Logcat is the following:
error ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{429ffe38: android.os.BinderProxy@429ffdd8}}
注意,之前出现此错误用户已pressed按钮,其实,经过调试,我想通了,它来自于这条线code:
Notice that this error appears before that the user has pressed the button, in fact, through debugging, I figured out it comes from this line code:
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());
}
}
}
下面的其它重要的方法:
Here the other important methods:
@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();
}
}
}
在这里,code在AndroidManifest.xml
Here the code in the 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!
推荐答案
由于在我的项目中,有很多问题登入Google+例,当创建 mGoogleApiClient
,你应该
As in my project, there are many problems with Google+ sign in. Example, when create mGoogleApiClient
, you should
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.addScope(Plus.SCOPE_PLUS_PROFILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
与 SCOPE_PLUS_PROFILE
- OAuth 2.0范围用于访问用户的Google+个人资料
with SCOPE_PLUS_PROFILE
- OAuth 2.0 scope for accessing the user's Google+ profile data.
您必须启用Google+ API,并创建SHA1和你的包凭证。
You need to enable Google+ API and create Credentials with SHA1 and your package.
通过OAuth同意画面您的电子邮件和产品名称(必填)
OAuth Consent screen with your email and product name (required)
更多的,请确认:
的applicationID 在标签&lt;用途&gt;
在AndroidManifest.xml
包名
包名在谷歌开发者控制台
More, make sure:
applicationId in tag <application>
package name in AndroidManifest.xml
package name in Credentials in Google Developer Console
所有相同
更多的,请确保您有权限:
all same
More, make sure you have the permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
更多,在方法
@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意向数据{
...
...
}
不要需要调用的 super.onActivityResult (要求code,结果code,数据);
DO NOT need call super.onActivityResult(requestCode, resultCode, data);
和件事也很重要:调试密钥库和发布密钥库
And more thing also important: debug keystore and release keystore.
和更多.....
这篇关于Android版Google+帐户登录不起作用 - 状态code = SIGN_IN_REQUIRED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!