Google Play 登录一直失败异常:4
Google Play Sign in keeps failing Exception: 4
所以我已经尝试了很长时间让我的应用程序具有 google 玩游戏的成就,但我无法让它工作。我一直在遵循本指南:https://developers.google.com/games/services/android/signin#implementing_player_sign-in 但没有成功,每次我打开我的应用程序时,它都会启动登录过程,但随后会停止并显示失败。这可能是我错过的东西,但我还是想不通。
一些代码的时间:
build.gradle:
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.google.code.gson:gson:2.8.0'
implementation "com.google.android.gms:play-services-games:12.0.1"
implementation "com.google.android.gms:play-services:12.0.1"
implementation 'com.android.support:multidex:1.0.3'
}
清单:
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.one.second">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:label="One Second"
android:icon="@drawable/app_icon"
android:theme="@style/AppTheme">
<meta-data android:name="com.google.android.gms.games.APP_ID"
android:value="@string/app_id" />
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".LowestActivity"
android:configChanges="orientation|screenSize"
android:screenOrientation="portrait"/>
</application>
main.java 中重要的部分:
private static final int RC_SIGN_IN = 9001;
@Override
protected void onCreate(Bundle _savedInstanceState) {
...
signInSilently();
}
private void signInSilently() {
GoogleSignInClient signInClient = GoogleSignIn.getClient(this, GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
signInClient.silentSignIn().addOnCompleteListener(this,
new OnCompleteListener<GoogleSignInAccount>() {
@Override
public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
if (task.isSuccessful()) {
Log.d(TAG, "Success signInSilently");
GoogleSignInAccount signedInAccount = task.getResult();
} else {
Log.d(TAG, "Failed signInSilently");
startSignInIntent();
}
}
});
}
private void startSignInIntent() {
Log.d(TAG, "startSignInIntent()");
GoogleSignInClient signInClient = GoogleSignIn.getClient(this,
GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
Intent intent = signInClient.getSignInIntent();
startActivityForResult(intent, RC_SIGN_IN);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
Log.w(TAG, "signInResult:success");
} catch (ApiException e) {
Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
signInSilently();
}
}
ids.xml
...
<!-- app_id -->
<string name="app_id" translatable="false">874********</string>
...
见图:
根据我的经验,此错误可能由 3 个不同的原因引起:
您需要手动登录才能使用 silentSignIn。
您尚未将用于测试的电子邮件添加到 Google Play 游戏控制台中的“测试人员”选项卡(或启用所有电子邮件)。
您的构建未使用与您的 Google Play 游戏应用 ID 生成时相同的 SHA1 密钥进行签名
参考文献:
ApiException on silent signing using GoogleSignInClient on Android
所以我已经尝试了很长时间让我的应用程序具有 google 玩游戏的成就,但我无法让它工作。我一直在遵循本指南:https://developers.google.com/games/services/android/signin#implementing_player_sign-in 但没有成功,每次我打开我的应用程序时,它都会启动登录过程,但随后会停止并显示失败。这可能是我错过的东西,但我还是想不通。
一些代码的时间:
build.gradle:
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.google.code.gson:gson:2.8.0'
implementation "com.google.android.gms:play-services-games:12.0.1"
implementation "com.google.android.gms:play-services:12.0.1"
implementation 'com.android.support:multidex:1.0.3'
}
清单:
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.one.second">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:label="One Second"
android:icon="@drawable/app_icon"
android:theme="@style/AppTheme">
<meta-data android:name="com.google.android.gms.games.APP_ID"
android:value="@string/app_id" />
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".LowestActivity"
android:configChanges="orientation|screenSize"
android:screenOrientation="portrait"/>
</application>
main.java 中重要的部分:
private static final int RC_SIGN_IN = 9001;
@Override
protected void onCreate(Bundle _savedInstanceState) {
...
signInSilently();
}
private void signInSilently() {
GoogleSignInClient signInClient = GoogleSignIn.getClient(this, GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
signInClient.silentSignIn().addOnCompleteListener(this,
new OnCompleteListener<GoogleSignInAccount>() {
@Override
public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
if (task.isSuccessful()) {
Log.d(TAG, "Success signInSilently");
GoogleSignInAccount signedInAccount = task.getResult();
} else {
Log.d(TAG, "Failed signInSilently");
startSignInIntent();
}
}
});
}
private void startSignInIntent() {
Log.d(TAG, "startSignInIntent()");
GoogleSignInClient signInClient = GoogleSignIn.getClient(this,
GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
Intent intent = signInClient.getSignInIntent();
startActivityForResult(intent, RC_SIGN_IN);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
Log.w(TAG, "signInResult:success");
} catch (ApiException e) {
Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
signInSilently();
}
}
ids.xml
...
<!-- app_id -->
<string name="app_id" translatable="false">874********</string>
...
见图:
根据我的经验,此错误可能由 3 个不同的原因引起:
您需要手动登录才能使用 silentSignIn。
您尚未将用于测试的电子邮件添加到 Google Play 游戏控制台中的“测试人员”选项卡(或启用所有电子邮件)。
您的构建未使用与您的 Google Play 游戏应用 ID 生成时相同的 SHA1 密钥进行签名
参考文献:
ApiException on silent signing using GoogleSignInClient on Android