简体   繁体   English

Facebook Android SDK 3.0登录问题

[英]Facebook android sdk 3.0 login issue

I have recently upgraded my code to use FB android SDK 3.0. 我最近将代码升级为使用FB android SDK 3.0。 My application has four activities and DOES NOT use fragments. 我的应用程序有四个活动,并且不使用片段。 Strangely my code works without any problem on android 2.3.4. 奇怪的是,我的代码在android 2.3.4上没有任何问题。 But in android 4.0.4, the state of session is always 'OPENING'. 但在android 4.0.4中,会话状态始终为“ OPENING”。 I'm using UiLifeCycleHelper and below is the the code in launcher activity. 我正在使用UiLifeCycleHelper,下面是启动器活动中的代码。

SharedPreferences mPrefs;
String access_token = mPrefs.getString("access_token", null);

Session currentSession = Session.getActiveSession();
if(currentSession == null || currentSession.getState().isClosed())
{
    if(access_token!=null)
    {
        SharedPreferences.Editor editor = mPrefs.edit(); 

    //One time upgrade from FB SDK 2.0 to 3.0
        editor.putString("access_token", null);
        editor.commit();

        AccessToken new_access_token = AccessToken.createFromExistingAccessToken(access_token, null, null, null, null);
        currentSession.open(new_access_token, myCallback);
        Session.setActiveSession(currentSession);
    }
    else
    {
        Session.openActiveSession(this, true, myCallback);
    }
}
else if (!currentSession.isOpened())
{
     OpenRequest openRequest = new OpenRequest(this).setCallback(myCallback);
     openRequest.setDefaultAudience(SessionDefaultAudience.FRIENDS);
     openRequest.setPermissions(Arrays.asList(permissions));
     openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
     currentSession.openForRead(openRequest);

}
else if(currentSession.isOpened())
{
    handleRequest(currentSession);

}


StatusCallback myCallback = new StatusCallback()
{

    @Override
    public void call(Session session, SessionState state, Exception exception)
    {
        handleRequest(session);

    }
};


public void handleRequest(Session activeSession)
{

    Request new_request = new Request(activeSession, "/me/friends", null, null, new Callback()
    {

        @Override
        public void onCompleted(Response response)
        {

            //Process the response and make UI changes.
        }

});
}

First I suspected that the problem was due to an old version Facebook app (1.9). 首先,我怀疑问题是由于旧版Facebook应用(1.9)引起的。 So I upgraded to latest version(3.2). 所以我升级到了最新版本(3.2)。 Still the problem persists on android 4.0.4. 问题仍然存在于Android 4.0.4上。

Here is my code in onActivityResult callback 这是我在onActivityResult回调中的代码

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
        uihelper.onActivityResult(requestCode, resultCode, data);
}

Is there anything wrong? 有什么问题吗?

Did you remember to add the onActivityResult code? 您还记得添加onActivityResult代码吗?

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

Also give a look at this other question 也看看另一个问题

I solved it myself. 我自己解决了。 Just commented out handleRequest in myCallback. 刚刚在myCallback中注释掉了handleRequest。

StatusCallback myCallback = new StatusCallback()
{

    @Override
    public void call(Session session, SessionState state, Exception exception)
    {
    //handleRequest(session);

    }
};

This time my app work without much problems on both android 2.3.4 and 4.0.4. 这次,我的应用在android 2.3.4和4.0.4上都没有太大问题。

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

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