Prog_kriss
Prog_kriss

Reputation: 31

PlayGames ERROR_INVALID_CREDENTIAL

I am trying to use the PlayGames login services from my app with firebase, but when I do, this error always appears:

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

Exception error:

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" } } ]

my code:

    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();
                }
            }
        });
}

what means malformed?, I have to add more information to the credential?

Upvotes: 0

Views: 848

Answers (1)

Prog_kriss
Prog_kriss

Reputation: 31

Ok I resolved the error:

1- I was using an incorrect variable "DEFAULT_SIGN_IN". I had to change it for "DEFAULT_GAMES_SIGN_IN" (after this, I got 12501 error, but it was resolved with the next steps)

2- I had to add this 2 lines in AndroidManifest.xml in Application tag:

<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- I have to add my APP ID in string.xml:

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

The app id its in your google console-> Game service-> your published service, on top (under the game title)

After doing this 3 steps, the app can connect to Play Game services.

Upvotes: 0

Related Questions