在我的项目之前,我使用了Google plus授权,但是现在我想用玩游戏登录方法代替它。我在Firebase文档中找到了有关此信息。但是,在阅读了这些信息之后,我没发现什么。
1)如何处理从启动Intent返回的结果?之前我使用下面的代码来处理google plus结果:
if (requestCode == RC_GOOGLE_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google Sign in failed", e);
updateUI(null);
}
}
2)如何启动它?我将以下代码用于google plus授权:
// Configure Google Sign In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(mActivity, gso);
// This method called when user push google sign in button
private void googleSignIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_GOOGLE_SIGN_IN);
}
3)如何注销?我想这段代码与玩游戏是一样的。
// Firebase sign out
mAuth.signOut();
// Google sign out
mGoogleSignInClient.signOut().addOnCompleteListener(mActivity,
task -> updateUI(null));
我的firebaseAuthWithPlayGames方法,我需要从OnactivityResult调用该方法:
private void firebaseAuthWithPlayGames(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithPlayGames:" + acct.getId());
String authCode = acct.getServerAuthCode();
if (null == authCode) {
String errorMsg = "firebaseAuthWithPlayGames: authCodeNull - Failure";
Log.d(TAG, errorMsg);
showToast(errorMsg);
return;
}
showProgressDialog();
AuthCredential credential = PlayGamesAuthProvider.getCredential(authCode);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(mActivity, 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());
showToast("Authentication failed.");
updateUI(null);
}
hideProgressDialog();
});
}
您知道使用此授权方法的任何示例吗?我看了官方的auth示例,但是没有玩游戏的授权。