如何使用Google提供程序在Firebase上重新validation用户?

Firebase中使用reauthenticate() 的示例仅显示如何重新validation使用电子邮件和密码签名的用户

 AuthCredential credential = EmailAuthProvider.getCredential("user@example.com", "password1234"); FirebaseAuth.getInstance().getCurrentUser().reauthenticate(credential); 

我也知道如何使用Facebook Provider重新进行身份validation( credential = FacebookAuthProvider.getCredential(AccessToken.getCurrentAccessToken().toString()) )。

问题是Google API中没有等效方法来获取当前的访问令牌并最终获得AuthCredential 。 那么在这种情况下我将传递给getCredential()

Related of "如何使用Google提供程序在Firebase上重新validation用户?"

考虑到您已收到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() {... 

您可以通过2种方式获取GoogleSignInResult进行身份validation。

i)通过在谷歌登录界面输入电子邮件ID和密码。

ii)从电话中已登录的帐户中选择帐户。

我使用第二种方法来获取访问令牌并validation用户身份。

更多支持参考链接如下。

谷歌登录链接1

Stackoverflow – 令牌刷新

谷歌身份validation提供程序文档

服务器端令牌validation文档

如果你唯一的目标是获得令牌,那么你也可以试试这个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. } } 

//获得身份validation

 AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); FirebaseAuth.getInstance().getCurrentUser().reauthenticate(credential) .addOnCompleteListener(new OnCompleteListener() { // add your job here on authenticated } // if token is obsoleted then you can do this credential.refreshToken(); accessToken = credential.getAccessToken();