使用Firebase无法登录Google Play游戏

时间:2018-06-20 22:11:57

标签: firebase android-studio firebase-authentication google-play-games

您好,这是我的应用代码,我尝试通过Google Play游戏登录,但无法使用。希望您能告诉我一些解决方案。我正在使用Google登录名,但正在进行另一项活动,但Play游戏登录名不起作用。

public class start extends AppCompatActivity implements
        View.OnClickListener{

    private static final String TAG = "StartActivity";

    GoogleApiClient mGoogleApiClient;
    GoogleSignInClient mGoogleSignInClient;


 SignInButton signInButton;

    private AdView mAdview;


    private InterstitialAd interstitialAd;

    private FirebaseAnalytics mFirebaseAnalytics;

    private ProgressDialog loadingBar;
    private Status statusTextView;

    TextView LoginUserName, LoginUserEmail;


    private FirebaseAuth mAuth;
    FirebaseAuth.AuthStateListener mAuthListener;

    private Button LogOutBtn;
    private Button playgames;


    private TextView mStatusTextView;
    private TextView mDetailTextView;
    private TextView mPlayername;


    private boolean mResolvingConnectionFailure = false;
    private boolean mAutoStartSignInflow = true;
    private boolean mSignInClicked = false;


    private final static int RC_SIGN_IN = 9001;

    @Override
    protected void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(mAuthListener);
        FirebaseUser currentUser = mAuth.getCurrentUser();
        signInSilently();
        mGoogleApiClient.connect();

        updateUI(currentUser);

    }


//

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);


        signInButton = (SignInButton) findViewById(R.id.sign_in_button);
        LogOutBtn = findViewById(R.id.logout);
        mAuth = FirebaseAuth.getInstance();
        mStatusTextView = findViewById(R.id.status);
        mDetailTextView = findViewById(R.id.detail);
        mPlayername = findViewById(R.id.mPlayername);
        playgames = findViewById(R.id.playgames);


        signInButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(start.this, SignIn.class));
            }
        });

        LogOutBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(start.this, SignIn.class));
            }
        });

        playgames.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startSignInIntent();
            }
        });



        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
                .requestServerAuthCode(XXXXXX)
                .requestEmail()
                .requestProfile()
                .requestIdToken(XXXXXX)
                .build();

        mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                        Toast.makeText(start.this, "Something went wrong", Toast.LENGTH_SHORT).show();
                    }
                })
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();




        //End of Authentification


    }


    // Call this both in the silent sign-in task's OnCompleteListener and in the
// Activity's onActivityResult handler.
    private void firebaseAuthWithPlayGames(GoogleSignInAccount acct) {
        Log.d(TAG, "firebaseAuthWithPlayGames:" + acct.getId());

        AuthCredential credential = PlayGamesAuthProvider.getCredential(acct.getServerAuthCode());
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "signInWithCredential:success");
                            FirebaseUser user = mAuth.getCurrentUser();
                            updateUI(user);
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                            Toast.makeText(start.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                            updateUI(null);
                        }

                        // ...
                    }
                });
    }




    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()) {
                            // The signed in account is stored in the task's result.
                            GoogleSignInAccount signedInAccount = task.getResult();
                            firebaseAuthWithPlayGames(signedInAccount);
                        } else {
                            // Player will need to sign-in explicitly using via UI
                            Toast.makeText(start.this, "Cannot Connect", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        signInSilently();
    }

    private void 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) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            if (result.isSuccess()) {
                // The signed in account is stored in the result.
                GoogleSignInAccount signedInAccount = result.getSignInAccount();
                firebaseAuthWithPlayGames(signedInAccount);
            } else {
                String message = result.getStatus().getStatusMessage();
                if (message == null || message.isEmpty()) {
                    Toast.makeText(start.this, "Cannot Connect 1", Toast.LENGTH_SHORT).show();
                }
                new AlertDialog.Builder(this).setMessage(message)
                        .setNeutralButton(android.R.string.ok, null).show();
            }
        }
    }


    private void updateUI(FirebaseUser user) {
        if (user != null) {
            String playerName = user.getDisplayName();
            mPlayername.setText(playerName);

// The user's Id, unique to the Firebase project.
// Do NOT use this value to authenticate with your backend server, if you
// have one; use FirebaseUser.getIdToken() instead.

            findViewById(R.id.sign_in_button).setVisibility(View.GONE);
        } else {

            findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sign_in_button:
                startActivity(new Intent(start.this, SignIn.class));
                break;
            case R.id.logout:
                startActivity(new Intent(start.this, SignIn.class));

                break;

            case R.id.playgames:
                startSignInIntent();
                break;

                // ...
        }
    }



}

我真的希望有人能提供一个可行的示例,或者可以在这里为我提供帮助,因为我在此代码上停留了一个月。

0 个答案:

没有答案