即使在首次登录后,Facebook AccessToken.getAccessToken 在打开应用程序时为空 [英] Facebook AccessToken.getAccessToken is null on opening of app even after first login

查看:21
本文介绍了即使在首次登录后,Facebook AccessToken.getAccessToken 在打开应用程序时为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经集成了最新的 Facebook android sdk (Sdk 4.0).这是我在 onCreate 方法中添加的代码.

I have integrated latest Facebook android sdk (Sdk 4.0). This is the code I have added in my onCreate method.

FacebookSdk.sdkInitialize(this.getApplicationContext());
            callbackManager = CallbackManager.Factory.create();
            if(AccessToken.getCurrentAccessToken()!=null){
                Log.d(FBTAG,"facebook already logged in");
                isFBLogin = true;
            }
            LoginManager.getInstance().registerCallback(callbackManager,
                    new FacebookCallback<LoginResult>() {
                        @Override
                        public void onSuccess(LoginResult loginResult) {
                            // App code
                            Log.d(FBTAG,"facebook log in");
                            isFBLogin = true;
                        }

                        @Override
                        public void onCancel() {
                             // App code
                            isFBLogin = false;
                        }

                        @Override
                        public void onError(FacebookException error) {
                            isFBLogin = false;
                            Log.d(FBTAG,"facebook login error: "+error);
                            // App code

                        }
            });

这是我用于 onClickLogin 的代码

And this is the code I have used for onClickLogin

public void onClickLogin() {
        LoginManager.getInstance().logInWithPublishPermissions(this, PERMISSIONS);
    }

我可以通过单击登录按钮并处理 onClickLogin 函数来登录.现在下次我打开应用程序时,我正在检查 AccessToken.getAccessToken 以检查用户是否已经登录到 facebook 但它总是为空.无论如何在新的 sdk 中没有在后台登录,这样我就不必要求用户总是像以前在 session 类中的版本那样登录.

I am able to login by clicking on the login button and processing onClickLogin function. Now next time I am opening the app the app I am checking for AccessToken.getAccessToken to check if the user is already logged in at facebook but it is always coming as null. Isn't there anyway in the new sdk to login in the background so that I don't have to ask the user to login always like it used to be in the previous version in session class.

推荐答案

LoginManager 返回的访问令牌将保存在共享首选项中,因此下次打开应用程序时,AccessToken.getCurrentAccessToken() 应该具有相同的访问令牌,这与使用 Session 类的方式相同.您可以查看 SDK 提供的示例以查看它们的工作情况.

The access token that was returned by the LoginManager will be saved in shared preferences, so the next time the app is opened, AccessToken.getCurrentAccessToken() should have the same access token, this is the same as how it with the Session class. You can check out the samples provided with the SDK to see them work.

确保您没有在会话之间重新安装应用程序,或将当前访问令牌显式设置为 null.

Make sure you're not reinstalling the app between sessions, or setting the current access token to null explicitly.

这篇关于即使在首次登录后,Facebook AccessToken.getAccessToken 在打开应用程序时为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆