简体   繁体   English

Android Facebook SDK 4.0无Facebook App登录

[英]Android Facebook SDK 4.0 Login without Facebook App

I'm having issues with the webview login for Facebook on Android. 我在Android上使用Facebook的webview登录时遇到问题。

I've followed the tutorials and login works perfectly when the user has the Facebook app installed. 当用户安装了Facebook应用程序时,我已经按照教程和登录工作完美。 When the Facebook app is not installed, the webview for facebook login pops up; 未安装Facebook应用程序时,会弹出facebook登录的webview; however, after logging in and accepting the permissions, the webview simply redirects back to the login screen. 但是,登录并接受权限后,webview只会重定向回登录屏幕。 It never goes back to my app. 它永远不会回到我的应用程序。

Has anyone else encountered this problem? 还有其他人遇到过这个问题吗?

    FacebookSdk.sdkInitialize(this);
    profileTracker = new ProfileTracker() {
        @Override
        protected void onCurrentProfileChanged(Profile profile, Profile profile2) {
            if (profile2 != null) {
                loggedIn(profile2);
            } else {
                loggedOut();
            }
        }
    };
    accessTokenTracker = new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(AccessToken accessToken, AccessToken accessToken2) {
            Profile.fetchProfileForCurrentAccessToken();
        }
    };
    callbackManager = CallbackManager.Factory.create();
    LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    // App code
                    getProfileInfo();
                }

                @Override
                public void onCancel() {
                    // App code
                    Log.e("Facebook Login", "Login Cancelled");
                    loggedOut();
                }

                @Override
                public void onError(FacebookException exception) {
                    // App code
                    Log.e("Facebook Login", "Failed to Login " + exception.toString());
                    loggedOut();
                }
            });

Looking at the logs without filters while the login takes place, I see a couple of possibly relevant logs. 在登录时查看没有过滤器的日志,我看到了几个可能相关的日志。

I/chromium﹕ [INFO:CONSOLE(0)] "event.returnValue is deprecated. Please use the standard event.preventDefault() instead.", source:  (0)
I/Auth.Core﹕ [TokenCache] Missing snowballing token: no granted scopes set.

I think you forgot to use callbackManager for proper workflow. 我想您忘记使用callbackManager进行正确的工作流程。 Used CallbackManager for registration callback must be called in onActivityResult of host activity. 必须在主机活动的onActivityResult中调用用于注册回调的已使用CallbackManager。 Activity should not be in singleInstance launchMode because it will not be able to launch startActivityForResult(facebook internally launches FacebookActivity using this method). Activity不应该在singleInstance launchMode中,因为它无法启动startActivityForResult(facebook在内部使用此方法启动FacebookActivity)。 So add to your activity: 所以添加到您的活动中:

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

What was causing the problem was that I was overriding the onclicklistener of the login button to call the login function of the LoginManager. 造成这个问题的原因是我重写了登录按钮的onclicklistener来调用LoginManager的登录功能。 Just don't. 只是不要。

Given your description, I am imagining that you either are not connected on the internet or that you are not handling the login callbacks. 根据您的描述,我想象您要么没有在互联网上连接, 要么您没有处理登录回调。

Here is a full facebook 4.0.+ login example . 这是一个完整的facebook 4.0。+登录示例

Edit Based on your code: 编辑根据您的代码:

You need to use FacebookSdk.sdkInitialize(this); 你需要使用FacebookSdk.sdkInitialize(this); before setContentView() . setContentView() 之前

This is a working example without using Facebook app and if you close your app and open it again you'll be logged in automatically unless you log out. 这是一个不使用Facebook应用程序的工作示例,如果您关闭应用程序并再次打开它,除非您注销,否则您将自动登录。 Here we get the user email address and friends list after log in. 在这里,我们在登录后获得用户电子邮件地址和朋友列表。

public class MainActivity extends Activity {

    private CallbackManager callbackManager;
    private LoginButton loginButton;
    private static final String TAG = "logTag";
    private TextView mFriend, mEmail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();

        setContentView(R.layout.activity_main);
        mFriend = (TextView) findViewById(R.id.tv_friend);
        mEmail = (TextView) findViewById(R.id.tv_email);

        loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setReadPermissions(Arrays.asList("public_profile, email, user_birthday, user_friends"));
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                Log.d(TAG, "Successful Login");
                GraphRequest friendsRequest = GraphRequest.newMyFriendsRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONArrayCallback() {
                            @Override
                            public void onCompleted(JSONArray objects, GraphResponse response) {


                                String x = objects.opt(0).toString();
                                mFriend.setText(x);

                            }
                        });
                GraphRequest meRequest = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                                try {
                                    String email = response.getJSONObject().getString("email").toString();
                                    mEmail.setText(email);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });

                Bundle parameters = new Bundle();
                parameters.putString("fields", "id, name, email, gender");
                meRequest.setParameters(parameters);
                meRequest.executeAsync();

                parameters = new Bundle();
                parameters.putString("field", "user_friends");
                friendsRequest.setParameters(parameters);
                friendsRequest.executeAsync();

            }

            @Override
            public void onCancel() {
                Log.d(TAG, "Login Canceled");
            }

            @Override
            public void onError(FacebookException error) {
                Log.d(TAG, "Login Error");
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        // manage login results
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
}

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

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