gpt4 book ai didi

java - firebase facebook 登录时出现身份验证失败错误

转载 作者:太空宇宙 更新时间:2023-11-04 10:56:57 26 4
gpt4 key购买 nike

我正在开发 firebase 演示应用程序,其中添加了 firebase facebook 以及 google 登录,因此 google 工作正常,但每当我尝试使用 facebook 登录时,它都会在运行时抛出身份验证失败,因此我将尝试所有 stackOverflow 解决方案,但它不起作用,所以有人知道如何解决这个问题吗?

以下是我的登录代码:-

public class SplashScreen extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {

//constant values
public static final String USER_NAME="com.gaurav.javascripttutorial.username";
public static final String USER_IMAGE="com.gaurav.javascripttutorial.userimage";
public static final String USER_ID="com.gaurav.javascripttutorial.userid";

int RC_SIGN_IN = 1000;

//Firebase
FirebaseAuth mAuth;
GoogleApiClient mGoogleApiClient;
CallbackManager mCallbackManager;

//views
private SignInButton googleLogin;
private LoginButton facebookLogin;
private ProgressDialog dialog;

//Shared Preference
SharedPreferences pref;
SharedPreferences.Editor editor;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.setApplicationId(getString(R.string.fb_app_id));
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.main_splashscreen);
googleLogin = (SignInButton) findViewById(R.id.googleLogin);
facebookLogin = (LoginButton) findViewById(R.id.facebookLogin);

pref = this.getSharedPreferences("user",0);
editor = pref.edit();

dialog = new ProgressDialog(this);
dialog.setMessage("Loading, Please wait....");

//Google configuration
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.requestProfile()
.build();

mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API,gso)
.build();

//Facebook configuration
mCallbackManager = CallbackManager.Factory.create();

mAuth = FirebaseAuth.getInstance();

if (mAuth.getCurrentUser()!= null){
FirebaseUser user = mAuth.getCurrentUser();
editor.clear();
editor.putString(USER_NAME,user.getDisplayName());
editor.putString(USER_ID, user.getUid());
editor.putString(USER_IMAGE, user.getPhotoUrl().toString());
editor.commit();
Intent intent = new Intent(SplashScreen.this, MainActivity.class);
startActivity(intent);
finish();
}

googleLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
googleSignIn();
}
});
facebookLogin.setReadPermissions("email","public_profile");
facebookLogin.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
handleFacebookAccessToken(loginResult.getAccessToken());
}

@Override
public void onCancel() {
Toast.makeText(SplashScreen.this, "Facebook login cancel.", Toast.LENGTH_LONG).show();
}

@Override
public void onError(FacebookException error) {
Toast.makeText(SplashScreen.this, "Something went wrong! Please try again", Toast.LENGTH_LONG).show();
}
});
}

private void googleSignIn(){
Intent intent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(intent,RC_SIGN_IN);
}

private void handleFacebookAccessToken(AccessToken token){
final AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
FirebaseUser user = mAuth.getCurrentUser();
editor.clear();
editor.putString(USER_NAME,user.getDisplayName());
editor.putString(USER_ID, user.getUid());
editor.putString(USER_IMAGE, user.getPhotoUrl().toString());
editor.commit();
Intent intent = new Intent(SplashScreen.this, MainActivity.class);
startActivity(intent);
finish();
dialog.dismiss();
} else {
// If sign in fails, display a message to the user.
Toast.makeText(SplashScreen.this, "Authentication failed.", Toast.LENGTH_LONG).show();
}
}
});
}

private void firebaseAuthWithGoogle(GoogleSignInAccount account){
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(),null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
FirebaseUser user = mAuth.getCurrentUser();
editor.clear();
editor.putString(USER_NAME,user.getDisplayName());
editor.putString(USER_ID, user.getUid());
editor.putString(USER_IMAGE, user.getPhotoUrl().toString());
editor.commit();
Intent intent = new Intent(SplashScreen.this, MainActivity.class);
startActivity(intent);
finish();
dialog.dismiss();
}else {
Toast.makeText(SplashScreen.this, "Authentication Failed", Toast.LENGTH_SHORT).show();
}
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager.onActivityResult(requestCode, resultCode, data);

if (requestCode == RC_SIGN_IN){
if (resultCode == RESULT_OK) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
dialog.show();
if (result.isSuccess()) {
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);

} else {
Log.d("Status", String.valueOf(result.getStatus().getStatusCode()));
Toast.makeText(this, "SignIn failed", Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(this, "Result Code error", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(this, "Google Play Services error.", Toast.LENGTH_LONG).show();

}
}

最佳答案

假设您已正确配置所有内容(否则 read the docs ),这对我来说工作正常(Kotlin 代码,抱歉。如果您需要 Java,只需进行翻译):

我的Activity扩展了AppCompatActivity:

class ActivityLogin : AppCompatActivity() {

private val callbackManager: CallbackManager = CallbackManager.Factory.create()
private val mAuth: FirebaseAuth = FirebaseAuth.getInstance()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_splashscreen)

loginButton.setReadPermissions("email", "public_profile")
loginButton.registerCallback(callbackManager, object : FacebookCallback<LoginResult> {
override fun onSuccess(result: LoginResult) {
handleFacebookAccessToken(result.accessToken)
}

override fun onCancel() {
Toast.makeText(this@ActivityLogin, "Facebook Authentication canceled.", Toast.LENGTH_SHORT).show()
}

override fun onError(error: FacebookException?) {
Toast.makeText(this@ActivityLogin, "Facebook Authentication failed.", Toast.LENGTH_SHORT).show()
}
})
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
super.onActivityResult(requestCode, resultCode, data)
callbackManager.onActivityResult(requestCode, resultCode, data)
}

private fun handleFacebookAccessToken(token: AccessToken) {
val credential = FacebookAuthProvider.getCredential(token.token)
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
val mainIntent = Intent(this@ActivityLogin, ActivityFeed::class.java)
startActivity(mainIntent)
finish()
} else {
Toast.makeText(this@ActivityLogin, "Firebase Authentication failed.", Toast.LENGTH_SHORT).show()
}
}
}
}

如您所见,无需调用

FacebookSdk.setApplicationId(getString(R.string.fb_app_id));
FacebookSdk.sdkInitialize(getApplicationContext());

在您的 Activity 的 onCreate() 期间。

关于java - firebase facebook 登录时出现身份验证失败错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47265799/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com