我在我的Android应用程序中集成了Facebook和Google身份验证。 在启动应用程序时,我想检查用户是否使用Facebook或Google身份验证登录到应用程序。我使用以下代码在Facebook上取得了成功:
if (Profile.getCurrentProfile() != null && AccessToken.getCurrentAccessToken() != null){
Intent i = new Intent(Splash.this, SecondActivity.class);
startActivity(i);
finish();
}
但是谷歌没有成功。此外,我搜索了许多答案,但大多数都使用Firebase进行Google身份验证。
如何使用Google身份验证而非Firebase实现此目的。
帮助将不胜感激。 提前谢谢!
答案 0 :(得分:1)
我们可以使用GoogleSignInApi.silentSignIn()
方法检查登录凭据是否有效。
它返回一个OptionalPendingResult
对象,用于检查凭证是否有效。如果凭据有效OptionalPendingResult
,则isDone()
方法将返回true。
然后可以使用get方法立即获得结果(如果可用)。
OptionalPendingResult
的Android文档:
https://developers.google.com/android/reference/com/google/android/gms/common/api/OptionalPendingResult
GoogleSignInApi
的Android文档:
https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInApi
这里是用于检查凭据是否有效的代码。
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(google_api_client);
if (opr.isDone()) {
// If the user's cached credentials are valid, the
// OptionalPendingResult will be "done" and the
// GoogleSignInResult will be available instantly.
Log.d("TAG", "Got cached sign-in");
GoogleSignInResult result = opr.get();
handleSignInResult(result);
}
答案 1 :(得分:0)
@Override
public void onStart() {
super.onStart();
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (opr.isDone()) {
// If the user's cached credentials are valid, the OptionalPendingResult will be "done"
// and the GoogleSignInResult will be available instantly.
Log.d(TAG, "Got cached sign-in");
GoogleSignInResult result = opr.get();
handleSignInResult(result);
} else {
// If the user has not previously signed in on this device or the sign-in has expired,
// this asynchronous branch will attempt to sign in the user silently. Cross-device
// single sign-on will occur in this branch.
showProgressDialog();
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(GoogleSignInResult googleSignInResult) {
hideProgressDialog();
handleSignInResult(googleSignInResult);
}
});
}
}
答案 2 :(得分:0)
我一直对这个问题感到困惑,也很长时间没有找到合适的解决方案。结果证明解决方案很短:
String strProvider = FirebaseAuth.getInstance().
getAccessToken(false).getResult().getSignInProvider();
所以,if (strProvider.equals("password"))
那么身份验证是通过电子邮件 + 密码,
if (strProvider.equals("google.com"))
然后通过 Google 进行身份验证,
if (strProvider.equals("facebook.com"))
然后通过 Facebook 进行身份验证。
但是,使用这种单行代码,您可以获得一个异常,可以通过像这样添加 OnSuccessListener
来防止该异常:
mAuth = FirebaseAuth.getInstance();
mAuth.getAccessToken(false).addOnSuccessListener(new OnSuccessListener<GetTokenResult>() {
@Override
public void onSuccess(GetTokenResult getTokenResult) {
strProvider = getTokenResult.getSignInProvider();
}
});
答案 3 :(得分:0)
最简单的方法:
for (UserInfo user : auth.getCurrentUser().getProviderData()) {
if (user.getProviderId().contains("facebook.com")) IS_FACEBOOK_LOGIN = true;
else if (user.getProviderId().contains("google.com")) IS_GOOGLE_LOGIN = true;
}