简体   繁体   English

使用 Facebook 的 Android 应用程序登录不适用于已安装的 Facebook 应用程序

[英]Android Application Login with Facebook is not working with Facebook App installed

This code is working well when I uninstalled the Facebook App but didn't work with Facebook App installed.当我卸载 Facebook 应用程序但未安装 Facebook 应用程序时,此代码运行良好。 I'm using Facebook SDK 4.0.我正在使用 Facebook SDK 4.0。

This is my code这是我的代码

package com.example.nhp04.gqfood;
import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;



public class Login extends AppCompatActivity implements Animation.AnimationListener {

private String info = "";
private LoginButton loginButton;
private CallbackManager callbackManager;
private AccessTokenTracker tracker;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
        setContentView(R.layout.activity_login);
loginButton = (LoginButton)findViewById(R.id.login_button);



loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();
            Profile profile = Profile.getCurrentProfile();
            info = ("User ID: " + 

    loginResult.getAccessToken().getUserId() + "\n" + "Auth Token: " + loginResult.getAccessToken().getToken());
                }

                @Override
                public void onCancel() {
                    info = ("Login attempt canceled.");
                }

                @Override
                public void onError(FacebookException e) {
                    info = ("Login attempt failed.");
                }
            });
            System.out.println(info);
            tracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {

            }
        };
        tracker.startTracking();
    }
    }

this function for checking login此功能用于检查登录

public boolean isLoggedIn() {
    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    return accessToken != null;
}

this on Resume and on Stop methods这在 Resume 和 Stop 方法上

@Override
protected void onResume() {
    super.onResume();
    if (isLoggedIn()){
        Intent home = new Intent(this, home.class);
        startActivity(home);
    }
}

@Override
protected void onStop() {
    super.onStop();
    tracker.stopTracking();
    finish();
}

And this is my onActivityResult这是我的onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        Intent home = new Intent(this, home.class);
        startActivity(home);
    } else {
        Toast.makeText(getApplicationContext(), "Unable to login please check your internet connection",Toast.LENGTH_LONG).show();
    }
}

where is your onActivityResult() code.你的onActivityResult()代码在哪里。 In onActivityResult() you need to use callbackmanager .onActivityResult()你需要使用callbackmanager User below code:用户下面的代码:

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

}

above will work both in fragment/activity.以上都适用于片段/活动。 Make sure you have确保你有

1. facebook app installed on your testing device
2. In facebook developer account check whether you have mentioned 
- correct package name : refer your android project manifestfile.xml

- check that have you mentioned correct launcher class
- Check that you have given correct debug/release hash key

3. Cross check your facebook application id and that mentioned in your manifestfile.xml facebook meta data are same

In your code change below下面的代码更改中

create you callbackmanager after setContentView(...) ;setContentView(...)之后创建你的callbackmanager

change it to below FacebookSdk.sdkInitialize(getApplicationContext()); AppEventsLogger.activateApp(this); setContentView(R.layout.activity_login); callbackManager = CallbackManager.Factory.create();将其更改为以下FacebookSdk.sdkInitialize(getApplicationContext()); AppEventsLogger.activateApp(this); setContentView(R.layout.activity_login); callbackManager = CallbackManager.Factory.create(); FacebookSdk.sdkInitialize(getApplicationContext()); AppEventsLogger.activateApp(this); setContentView(R.layout.activity_login); callbackManager = CallbackManager.Factory.create();

Remember if this is with you facebook issue then your problem lies within this dont waste time in searching other thing.请记住,如果这与您的 Facebook 问题有关,那么您的问题就在于不要浪费时间搜索其他东西。 Also put log in failure method in callback of facebook sdk.还将登录失败方法放在 facebook sdk 的回调中。

Post comment if you still have problem如果您仍然有问题,请发表评论

you can remove your app in your facebook app.您可以在 Facebook 应用中删除您的应用。 like you can open facebook app in go setting>>account setting>>app>>youer App >> remove .就像您可以在 go setting>>account setting>>app>>youer App >> remove 中打开 facebook 应用程序。 after remove youer app it uninstall your app .and reinstall it and check login with facebook is working or not.删除您的应用程序后,它会卸载您的应用程序。并重新安装它并检查使用 facebook 登录是否有效。

请更改您的 facebook sdk 版本,在您清理并重建您的应用程序后,它将起作用

if you're getting an error message that looks like this: Error如果您收到如下所示的错误消息:错误

Invalid key hash.无效的密钥哈希。 The key hash "...5GAvm/gHi..." does not match any stored key hashes.密钥散列“...5GAvm/gHi...”与任何存储的密钥散列都不匹配。 Configure your app key hashes at https://developers.facebook.com/apps/...55.../https://developers.facebook.com/apps/...55.../配置您的应用程序密钥哈希

then copy the hash key provided in the error message and replace the old one with it and try again with the Facebook app installed on your device然后复制错误消息中提供的哈希键并用它替换旧的,然后使用您设备上安装的 Facebook 应用程序重试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM