android facebook post api无法正常工作

时间:2013-05-04 05:59:18

标签: android facebook facebook-android-sdk

我正在使用android facebook api在用户Feed上发布。

登录成功, 但函数publishstory不起作用。我有以下代码。

private void publishStory() {

        Session session = Session.getActiveSession();

        if (session != null){

            // Check for publish permissions    
            List<String> permissions = session.getPermissions();
            if (!isSubsetOf(PERMISSIONS, permissions)) {
                pendingPublishReauthorization = true;
                Session.NewPermissionsRequest newPermissionsRequest = new Session
                        .NewPermissionsRequest(this, PERMISSIONS);
            session.requestNewPublishPermissions(newPermissionsRequest);
                return;
            }

            Bundle postParams = new Bundle();
            postParams.putString("name", "Facebook SDK for Android");
            postParams.putString("caption", "Build great social apps and get more installs.");
            postParams.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
            postParams.putString("link", "https://developers.facebook.com/android");
            postParams.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");

            Request.Callback callback= new Request.Callback() {
                public void onCompleted(Response response) {
                    JSONObject graphResponse = response
                                               .getGraphObject()
                                               .getInnerJSONObject();
                    String postId = null;
                    try {
                        postId = graphResponse.getString("id");
                    } catch (JSONException e) {
                        Log.i("error",
                            "JSON error "+ e.getMessage());
                    }
                    FacebookRequestError error = response.getError();
                    if (error != null) {
                        Toast.makeText(FacebookActivity.this.getApplicationContext(),
                             error.getErrorMessage(),
                             Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(FacebookActivity.this
                                 .getApplicationContext(), 
                                 postId,
                                 Toast.LENGTH_LONG).show();
                    }
                }
            };

            Request request = new Request(session, "me/feed", postParams, 
                                  HttpMethod.POST, callback);

            RequestAsyncTask task = new RequestAsyncTask(request);
            task.execute();
        }

    }

问题是它执行到行

session.requestNewPublishPermissions(newPermissionsRequest);
                return;
            }

然后退出函数返回而不显示任何弹出窗口。 有什么想法吗?

3 个答案:

答案 0 :(得分:1)

参考此代码 - &gt;

FbShare Code Pattern Gist

答案 1 :(得分:0)

当您显示代码时,如果找到没有必要权限的访问令牌,则调用requestNewPublishPermissions()。 但是为了在请求之后完成操作,您必须定义Session.StatusCallback

private static Session.StatusCallback sessionCallback = new Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        Log.i(TAG, "callback");
        if (exception == null) {
            if (pendingPublishAction && state.isOpened()) {
                pendingPublishAction = false;
                //do your post action;
            }
        }
        else{
            //handle exception
        }
    }
};

但是,根据您的说法,似乎甚至没有显示新权限的对话框。你是否像这样实施了onActivityResult

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Session session = Session.getActiveSession();
    if (session != null) {
        session.onActivityResult(this, requestCode, resultCode, data);
    }
}

如果您这样做,在身份验证阶段肯定会出现其他类型的错误,在这种情况下,logcat应该显示一些内容。

答案 2 :(得分:0)

必须仅为片段活动调用返回我在正常活动中执行此操作,这会产生问题。