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

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

问题描述

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

  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){
//应用代码
Log.d(FBTAG,facebook登录);
isFBLogin = true;
}

@Override
public void onCancel(){
//应用程序代码
isFBLogin = false;
}

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

}
});

这是我用于onClickLogin的代码

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

我可以通过点击登录按钮并处理onClickLogin功能登录。现在下次我打开应用程序我正在检查AccessToken.getAccessToken的应用程序,以检查用户是否已经在Facebook上登录,但它始终为空。
在新的sdk中不存在登录的背景,所以我不必要求用户永远像以前在会话类中的旧版本一样登录。

解决方案

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



确保您不会在会话之间重新安装应用程序,或将当前访问令牌设置为null明确地。


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

                        }
            });

And this is the code I have used for onClickLogin

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

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.

解决方案

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.

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天全站免登陆