requestNewReadPermissions然后requestNewPublishPermissions后StatusCallback不叫不叫、requestNewReadPermissions

2023-09-04 10:54:06 作者:我给你的爱情难道不够、

我开发一个Android应用程序与Facebook的整合。我想:

I am developing an Android App that integrates with Facebook. I would like to:

让用户登录与Facebook 获取用户的电子邮件地址在Facebook上(可以是代理的电子邮件地址,这是罚款) 发布到代表他/她的用户的墙/时间线 Let the user login with Facebook Get the user's email address on Facebook (could be a proxied email address, which is fine) Post to the user's wall/timeline on his/her behalf

从技术上讲,这将是:

验证用户 请 电子邮件许可 请求的publish_stream许可

我叫Session.openActiveSession()以Session.StatusCallback (我已经检查过没有活动开了会议事前):

1. Authenticate the user

I called Session.openActiveSession() with a Session.StatusCallback (I already checked that there is no active opened session beforehand):

final Session.StatusCallback sessionStatusCallback = new Session.StatusCallback() {
    public void call(final Session session, SessionState state, Exception exception) {
        // If there is an exception...
        if(exception != null)
        {
            // Handle fail case here.
            return;
        }

        // If session is just opened...
        if(state == SessionState.OPENED)
        {
            // Handle success case here.
            return;
        }
    };
};

// Start Facebook Login.
Session.openActiveSession(activity, true, sessionStatusCallback);

我的回调是成功登录后调用。到目前为止好。

My callback is called after successful login. So far so good.

这是我的状态回调:

new Session.StatusCallback() {
    public void call(final Session session, SessionState state, Exception exception) {
        // If there is an exception...
        if(exception != null)
        {
            // Handle fail case here.
            return;
        }

        // If token is just updated...
        if(state == SessionState.OPENED_TOKEN_UPDATED)
        {
            // Handle success case here.
            return;
        }
    };
};

我请求与Session.requestNewReadPermissions():

final Session session = Session.getActiveSession();
final static String[] PERMISSION_ARRAY_READ = {"email"};
final List<String> permissionList = Arrays.asList(PERMISSION_ARRAY_READ);

// If all required permissions are available...
if(session.getPermissions().containsAll(permissionList))
{
    // Handle success case here.
    return;
}

// Request permissions.
session.requestNewReadPermissions(new Session.NewPermissionsRequest(activity, permissionList));

权限被授予后,我的回调被调用。到目前为止好。

My callback is called after permission is granted. So far so good.

这是我的状态回调:

new Session.StatusCallback() {
    public void call(final Session session, SessionState state, Exception exception) {
        // If there is an exception...
        if(exception != null)
        {
            // Handle fail case here.
            return;
        }

        // If token is just updated...
        if(state == SessionState.OPENED_TOKEN_UPDATED)
        {
            // Handle success case here.
            return;
        }
    };
};

我请求与Session.requestNewPublishPermissions():

final Session session = Session.getActiveSession();
final static String[] PERMISSION_ARRAY_PUBLISH = {"publish_stream"};
final List<String> permissionList = Arrays.asList(PERMISSION_ARRAY_PUBLISH);

// If all required permissions are available...
if(session.getPermissions().containsAll(permissionList))
{
    // Handle success case here.
    return;
}

// Request permissions.
session.requestNewPublishPermissions(new Session.NewPermissionsRequest(activity, permissionList));

这时候,我的回调的不可以权限授予后调用。

This time, my callback is not called after permission is granted.

经进一步调查,我发现,我的回调是由com.facebook.Session#postStateChange(SessionState, SessionState会,异常) :

Upon further investigation, I found that my callback is triggered by com.facebook.Session#postStateChange(SessionState, SessionState, Exception):

void postStateChange(final SessionState oldState, final SessionState newState, final Exception exception) {
    if (oldState == newState && exception == null) {
        return;
    }

    /* ... */
}

由于 oldState newState 是相等的(都是SessionState.OPENED_TOKEN_UPDATED,我的回调不叫。

Since oldState and newState are equal (both being SessionState.OPENED_TOKEN_UPDATED, my callback is not called.

我怎样才能得到许可之后的任何通知,被授予第2次?我应该为close()会话和re-open从缓存?

How can I receive any notification after permission is granted for the 2nd time? Am I supposed to close() the session and re-open it from cache?

我的Facebook的Andr​​oid SDK 3.0是从这里,这是在Facebook的陈述下载开始使用Facebook的SDK为Android 。

My Facebook Android SDK 3.0 is download from here, which is stated in Facebook's Getting Started with the Facebook SDK for Android.

推荐答案

这是一个错误。

你提到的解决方法是基本正确,但你并不需要调用close。如果您使用的是单一的活动会话,要求requestNewPublishPermissions(前)只要致电:

The workaround you mention is basically correct, though you do not need to call close. If you are using the single active session, before calling requestNewPublishPermissions() just call:

Session.openActiveSessionFromCache(myContext);

如果您正在使用多个会话,你需要初始化与TokenCachingStrategy一个新的Session,验证它是在CREATED_TOKEN_LOADED状态,并调用openForRead(空);

If you are using multiple sessions, you need to initialize a new Session with the TokenCachingStrategy, verify it is in the CREATED_TOKEN_LOADED state, and call openForRead(null);

在做其中的一个后,requestNewPublishPermissions()应一旦完成呼叫的通知。

After doing one of these, requestNewPublishPermissions() should call your notification once it completes.