reauthenticate()
的The example仅显示如何重新验证使用电子邮件和密码签名的用户:
AuthCredential credential = EmailAuthProvider.getCredential("user@example.com", "password1234");
FirebaseAuth.getInstance().getCurrentUser().reauthenticate(credential);
我也知道如何使用Facebook提供商(credential = FacebookAuthProvider.getCredential(AccessToken.getCurrentAccessToken().toString())
)重新进行身份验证。
问题是Google API中没有等效方法来获取当前的访问令牌并最终得到AuthCredential
。那么在这种情况下我应该传递给getCredential()
?
答案 0 :(得分:4)
我知道这是个老问题,但是我没有找到完整的答案。这是在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.");
}
}
});
}
答案 1 :(得分:0)
考虑到您将收到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>() {...
答案 2 :(得分:0)
您可以通过2种方式获取GoogleSignInResult
进行身份验证。
i)通过在谷歌登录界面输入电子邮件ID和密码。
ii)从手机中已登录的帐户中选择帐户。
我使用第二种方法获取访问令牌并验证用户身份。
以获得更多支持参考链接。
google auth provider documentation
server side token verification docs
如果您唯一的目标是获取令牌,那么您也可以尝试this github source。
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();