我正在开发一个应用程序,在该应用程序中,我可以为用户提供通过Facebook登录的选项。现在,我正在为此使用Firebase,并且每次尝试登录时都会收到身份验证失败的消息。它将在下一个活动中进行,但是显示“身份验证失败”消息。 这是我的代码。这是我的主要活动:-
public class MainActivity extends AppCompatActivity {
private CallbackManager mCallbackManager;
private FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private TextView txtUser;
private TextView txtEmail;
private ImageView imgProfile;
private LoginButton logoutButton;
private LoginButton loginButton;
private static final String TAG = "MainActivity";
private FirebaseAuth mAuth;
PackageInfo info;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
// Initialize Facebook Login button
mCallbackManager = CallbackManager.Factory.create();
LoginButton loginButton = findViewById(R.id.login_button);
loginButton.setReadPermissions("email", "public_profile");
loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "facebook:onSuccess:" + loginResult);
handleFacebookAccessToken(loginResult.getAccessToken());
}
@Override
public void onCancel() {
Log.d(TAG, "facebook:onCancel");
// ...
}
@Override
public void onError(FacebookException error) {
Log.d(TAG, "facebook:onError", error);
// ...
}
});
}
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser!=null){
updateUI(currentUser);
}
}
private void updateUI(FirebaseUser currentUser) {
Toast.makeText(MainActivity.this,"Done",Toast.LENGTH_SHORT);
Intent intent = new Intent(MainActivity.this,AccountActivity.class);
startActivity(intent);
finish();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Pass the activity result back to the Facebook SDK
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
private void handleFacebookAccessToken(AccessToken token) {
Log.d(TAG, "handleFacebookAccessToken:" + token);
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
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}
// ...
}
});
}
}
现在,我已经使用以下方法获得了哈希密钥:-
try {
PackageInfo info = getPackageManager().getPackageInfo(
getPackageName(),
PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest messageDigest = MessageDigest.getInstance("SHA");
messageDigest.update(signature.toByteArray());
Log.d("KeyHash:", Base64.encodeToString(messageDigest.digest(), Base64.DEFAULT));
}
}
catch (PackageManager.NameNotFoundException e) {
}
catch (NoSuchAlgorithmException e) {
}
并将其放在我的fb开发人员控制台中。它仍然显示此错误。能否请任何人在这里解释我做错了什么?
当我检查logcat时,消息显示在这里:-“发生内部错误。[API密钥无效。请传递有效的API密钥”。 我什至不知道在哪里使用此API密钥。请帮忙。谢谢
答案 0 :(得分:0)
登录Firebase之前有一个步骤,您必须在Firebase控制台上注册您的应用程序。我还没有做到。 我创建了一个新项目,然后在firebase上进行了每个步骤。而且我现在没有任何错误。