1

我正在尝试使用来自 android 的 facebook sdk 发送请求。

我就是这样做的:

private void sendInviteRequestDialog() {
        Bundle params = new Bundle();
        params.putString("id", USER_ID);
        params.putString("to", "xxxx"); 
        showDialogWithoutNotificationBar("apprequests", params);
}

 WebDialog requestsDialog = (
                    new WebDialog.RequestsDialogBuilder(HelloActivity.this,
                            Session.getActiveSession(),
                            params))
                            .setOnCompleteListener(new OnCompleteListener() {

                                @Override
                                public void onComplete(Bundle values,
                                        FacebookException error) {
                                    if (error != null) {
                                        if (error instanceof FacebookOperationCanceledException) {
                                            Toast.makeText(HelloActivity.this.getApplicationContext(), 
                                                    "Request cancelled", 
                                                    Toast.LENGTH_SHORT).show();
                                        } else {
                                            Toast.makeText(HelloActivity.this.getApplicationContext(), 
                                                    "Network Error", 
                                                    Toast.LENGTH_SHORT).show();
                                        }
                                    } else {
                                        final String requestId = values.getString("request");
                                        if (requestId != null) {
                                            Toast.makeText(HelloActivity.this.getApplicationContext(), 
                                                "Request sent",  
                                                Toast.LENGTH_SHORT).show();
                                        } else {
                                            Toast.makeText(HelloActivity.this.getApplicationContext(), 
                                                    "Request cancelled", 
                                                    Toast.LENGTH_SHORT).show();
                                        }
                                    }   
                                }

                                })
                            .build();
            requestsDialog.show();

所以,我登录了 fb,我可以看到我的朋友列表,在我的墙上发帖,但我无法发送邀请到我的应用程序的请求。在计算机上的网络浏览器中使用的相同 url 可以工作,但不能在 android 应用程序中使用。这是我收到的错误:

An error occured. Please try again later.
API Error code: 110
API Error Description: Invalid user id
Error Message: Missing user cookie(to validate session user)

我的权限是:

private static final List<String> PERMISSIONS = Arrays.asList(
            "publish_actions", "publish_stream", "offline_access", "email",
            "basic_info", "user_likes", "user_birthday", "status_update",
            "create_note", "share_item", "user_status");

我此刻非常迷茫,不要明白这一点。任何想法都会很棒。谢谢你。

4

2 回答 2

1

我浪费了很多时间,但已经找到了解决方案。Facebook的问题是缓存。我为获取会话创建方法并清除缓存是从缓存创建令牌:

private Session getFBSession() {
    Session session = Session.getActiveSession();

    if (session == null) {
        session = new Session(getActivity());
        Session.setActiveSession(session);

        if (session.getState() == SessionState.CREATED_TOKEN_LOADED)
            session.closeAndClearTokenInformation();
    }

    return session;
}

但对你来说,这还不够,因为关闭的会话不是打开的会话。所以你需要在按钮按下后重新打开会话:

private void publishFeedDialog() {
    Session session = getFBSession();

    if (!session.isOpened())
        session.openForRead(new Session.OpenRequest(this).setCallback(facebookForShareCallback));
    else
        session.openActiveSession(getActivity(), this, true, facebookForShareCallback);
}

回调应该是这样的:

private Session.StatusCallback facebookForShareCallback = new Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        if (session.isOpened()) {
            Bundle params = new Bundle();
            params.putString("name", "name");
            params.putString("caption", event.title);
            params.putString("description", event.descLong);
            params.putString("link", "http://www.lt");
            params.putString("picture", event.logo);

            WebDialog feedDialog = new WebDialog.FeedDialogBuilder(getActivity(), session, params)
                    .build();

            feedDialog.show();
        } else if (state == SessionState.CLOSED_LOGIN_FAILED) {
            if (checkin != null)
                Toast.makeText(getActivity(), getString(R.string.facebook_error), Toast.LENGTH_LONG).show();
        }
    }
};
于 2014-05-01T19:38:06.237 回答
0

您也必须发送 Facebook 令牌

String Token = session.getAccessToken();

接着

    params.putString("token", Token);        
    params.putString("id", USER_ID);
    params.putString("to", "xxxx"); 

应该是作品

于 2013-07-10T09:49:21.300 回答