PlayGames ERROR_INVALID_CREDENTIAL

PlayGames ERROR_INVALID_CREDENTIAL

我正在尝试通过 firebase 使用我的应用程序中的 PlayGames 登录服务,但是当我这样做时,总是会出现此错误:

"com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The supplied auth credential is malformed or has expired."

异常错误:

The supplied auth credential is malformed or has expired. [ Failed to fetch resource from https://www.googleapis.com/games/v1/players/me, http status: 403, http response: { "error": { "errors": [ { "domain": "global", "reason": "forbidden", "message": "Forbidden" } ], "code": 403, "message": >"Forbidden" } } ]

我的代码:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestEmail()
        .requestServerAuthCode("xxx.apps.googleusercontent.com", true)
        .build();


    GoogleSignInClient signInClient = GoogleSignIn.getClient(this, gso);
    Intent intent = signInClient.getSignInIntent();
    startActivityForResult(intent, RC_SIGN_IN);

}
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);
        try {
            // Google Sign In was successful, authenticate with Firebase
            GoogleSignInAccount account = task.getResult(ApiException.class);
            firebaseAuthWithPlayGames(account);
        } catch (ApiException e) {
            // Google Sign In failed, update UI appropriately
            Log.w(TAG, "Google sign in failed", e);
        }
    }
}

private void firebaseAuthWithPlayGames(GoogleSignInAccount acct) {
    mAuth = FirebaseAuth.getInstance();
    FirebaseUser currentUser = mAuth.getCurrentUser();
    AuthCredential credential = PlayGamesAuthProvider.getCredential(acct.getServerAuthCode());
    Log.d(TAG, "firebaseAuthWithPlayGames4:" + acct.isExpired());
    mAuth.signInWithCredential(credential)
        .addOnCompleteListener(this, new OnCompleteListener < AuthResult > () {
            @Override
            public void onComplete(@NonNull Task < AuthResult > task) {
                if (task.isSuccessful()) {
                    Log.d(TAG, "signInWithCredential:success");
                    FirebaseUser user = mAuth.getCurrentUser();
                } else {
                    //here get the exception error always
                    Log.w(TAG, "signInWithCredential:failure", task.getException());
                    Toast.makeText(MainActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show();
                }
            }
        });
}

什么意思是格式错误?我必须向凭据添加更多信息?

好的,我解决了错误:

1- 我使用了不正确的变量 "DEFAULT_SIGN_IN"。我不得不将其更改为 "DEFAULT_GAMES_SIGN_IN"(在此之后,我收到 12501 错误,但已通过后续步骤解决)

2- 我必须在应用程序标签的 AndroidManifest.xml 中添加这两行:

<application ....>
    <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" />
</ application>

3- 我必须在 string.xml 中添加我的应用程序 ID:

<string name = "app_id"> xxxxxx815210 </ string>

应用程序 ID 在您的 google 控制台-> 游戏服务-> 您发布的服务中,在顶部(在游戏标题下)

完成这 3 个步骤后,应用程序可以连接到 Play 游戏服务。