在 Firebase 中使用 reauthenticate()
的示例仅显示如何重新验证使用 电子邮件和密码签名的用户:
AuthCredential credential = EmailAuthProvider.getCredential("[email protected]", "password1234");
FirebaseAuth.getInstance().getCurrentUser().reauthenticate(credential);
我也知道如何向 Facebook 提供商重新进行身份验证 (
credential = FacebookAuthProvider.getCredential(AccessToken.getCurrentAccessToken().toString())
)。
问题是 Google API 中没有等效的方法来获取当前的访问令牌并最终获得
AuthCredential
。那么在这种情况下我应该传递给 getCredential()
什么呢?
我知道这是一个老问题,但我没有找到完整的答案。这是在 Android 上执行此操作的方法。
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
// Get the account
GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(context);
if (acct != null) {
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
user.reauthenticate(credential).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "Reauthenticated.");
}
}
});
}
考虑到您会收到
GoogleSignInResult
作为登录响应,我认为您可以使用以下代码:
// assuming result variable has GoogleSignInResult
// GoogleSignInResult result
// Get the account
GoogleSignInAccount acct = result.getSignInAccount();
// credential
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
FirebaseAuth.getInstance().getCurrentUser().reauthenticate(credential).addOnCompleteListener(new OnCompleteListener<Void>() {...
更新的方法是使用
startActivityForReauthenticateWithProvider
他就是一个例子:
suspend fun reAuthenticateUserGoogle(activity: Activity) {
val provider = OAuthProvider.newBuilder("google.com")
firebaseAuth.currentUser?.startActivityForReauthenticateWithProvider(activity, provider.build())
.addOnSuccessListener {
// Reauthenticated successfully
}
.addOnFailureListener {
// Error
}
}
您可以通过2种方式认证获得
GoogleSignInResult
。
i) 通过在谷歌登录屏幕中输入电子邮件 ID 和密码。
ii) 从手机中已登录的帐户中选择帐户。
我使用了第二种方法来获取访问令牌并对用户进行身份验证。
下面给出了更多支持参考链接。
如果你唯一的目标是获得代币,那么你也可以尝试这个github源码。
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
//使用登录选项构建API客户端实例。
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent,RC_SIGN_IN); }
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN){
GoogleSignInResult result =Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
} else {
// Signed out, show unauthenticated.
}
}
// 获得身份验证
AuthCredential credential =
GoogleAuthProvider.getCredential(acct.getIdToken(), null);
FirebaseAuth.getInstance().getCurrentUser().reauthenticate(credential)
.addOnCompleteListener(new OnCompleteListener<Void>() {
// add your job here on authenticated
}
// if token is obsoleted then you can do this
credential.refreshToken();
accessToken = credential.getAccessToken();