【问题标题】:Post on FB wall Using 3.0 SDK使用 3.0 SDK 在 FB 墙上发帖
【发布时间】:2013-09-09 16:20:13
【问题描述】:

各位程序员,您好,我在使用新的 Facebook SDK 时遇到了困难,

场景是:

我正在使用片段,所以我按照以下步骤操作

Why doesn't Android Facebook interface work with Fragments?

现在我得到了 oncomplete 和令牌 ID

现在我想在墙上发帖,所以我创建了这个功能

private void publishStory(Session session) {

    if (session != null){

        // Check for publish permissions    
        session.addCallback(new StatusCallback() {

            @Override
            public void call(Session session, SessionState state,
                Exception exception) {
                List<String> PERMISSIONS = Arrays
                    .asList("publish_actions");
                session
                    .requestNewPublishPermissions(new Session.NewPermissionsRequest(
                        getActivity(), PERMISSIONS));

                Request request = Request.newStatusUpdateRequest(
                    session, "Temple Hello Word Sample",
                    new Request.Callback() {
                        @Override
                        public void onCompleted(Response response) {
                            Log.i("fb:done = ", response.getGraphObject() + ","
                                + response.getError());
                        }
                    });
                request.executeAsync();

            }

        });

并将声明放在 Oncomplete

    public void call(final Session session, SessionState state, Exception exception) {
            if (session.isOpened()) {
                Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
                    @Override
                    public void onCompleted(GraphUser user, Response response) {



                                                        publishStory(session);
                        }
                        else
                        {
                            Log.v("FACEBOOK ", "NO ACCESS");
                        }
                    }
                });
            }

我的问题出在这行代码session.addCallback(new StatusCallback() { 上,它跳过了下一步,从而结束了函数而没有在我的墙上张贴。

我很确定会话处于活动状态,因为我有 applicationID 和访问令牌..

有什么帮助吗?

谢谢

编辑

我根据下面的答案使用此代码并添加了新权限

private void publishStoryTEST(final Session session)
{       

    if (session != null) 
    {
        Bundle postParams = new Bundle();
        postParams.putString("message", "TEST");
        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("JSON", "JSON error " + e.getMessage());
                }
                FacebookRequestError error = response.getError();
                Log.e("post response", response.toString());
                if (error != null) 
                {
                } 
                else 
                {
                }

            }
        };
        List<String> PERMISSIONS = Arrays
                .asList("publish_actions");
        session.requestNewPublishPermissions(new Session.NewPermissionsRequest(getActivity(), PERMISSIONS));    
        Request request = new Request(session, "me/feed", postParams, HttpMethod.POST, callback);

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

    }
}

但我仍然收到错误提示

The user hasn't authorized the application to perform this action

为什么我仍然收到该错误?权限从来没有调用过吗?

【问题讨论】:

    标签: android facebook session


    【解决方案1】:

    这个问题很常见,我正在与您分享我目前在我的应用程序中使用的发布方法的工作源代码。

    private void publishStory(String status)
    {       
    Session session = Session.getActiveSession();
    
    if (session != null) 
    {
    Bundle postParams = new Bundle();
    postParams.putString("message", status);
    Request.Callback callback = new Request.Callback() 
        {
        public void onMalformedURLException(MalformedURLException e)
              {
    
              } 
        public void onIOException(IOException e) 
              {
    
              } 
        public void onFileNotFoundException(FileNotFoundExceptione) 
              {
    
              } 
        public void onFacebookError(FacebookError e) 
              {
    
              }
    
    public void onCompleted(Response response) 
    {
        JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();
        String postId = null;
        try 
        {
            postId = graphResponse.getString("id");
        } 
        catch (JSONException e) 
        {
            Log.i("JSON", "JSON error " + e.getMessage());
        }
        FacebookRequestError error = response.getError();
        Log.e("post response", response.toString());
        if (error != null) 
        {
        } 
        else 
        {
        }
    }
    };
    
    Request request = new Request(session, "me/feed", postParams, HttpMethod.POST, callback);
    RequestAsyncTask task = new RequestAsyncTask(request);
    task.execute();
    }
    }
    


    我希望这能解决您的问题。

    编辑:
    有关设置具有适当权限和发布功能的 Facebook SDK 3.0 的详细过程,请参阅以下链接。
    Post to facebook after login fails Android
    http://www.kpbird.com/2013/03/android-login-using-facebook-sdk-30.html

    【讨论】:

    • 仍然无法正常工作,我在这一行出现错误 JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();
    • 我可以跳过 Request.Callback 然后最后一步是 onComplete 吗?
    • 我收到一条错误消息,提示用户尚未授权应用程序执行此操作
    • 我认为,您没有提供在 Facebook 上发帖的特定权限。请查看更新后的答案,谢谢。
    • 虽然,如果你想手动处理登录注销的东西。然后我建议您使用Session 对象并检查Session 对象以检查用户当前状态并分别在注销/登录事件上结束或建立新的Sessions
    【解决方案2】:

    // 试试下面的代码

    private void postData() {
            lnrPbr.setVisibility(View.VISIBLE);
            isSharingData = true;
    
            Session session = Session.getActiveSession();
            if (session != null) {
    
                Bundle postParams = new Bundle();
                postParams.putString("name", getIntent().getStringExtra("IN_SHARE_CAPTION") == null ? "" : getIntent().getStringExtra("IN_SHARE_CAPTION"));
                postParams.putString("caption", getIntent().getStringExtra("IN_SHARE_CAPTION") == null ? "" : getIntent().getStringExtra("IN_SHARE_CAPTION"));
                postParams.putString("description", getIntent().getStringExtra("IN_SHARE_DESCRIPTION") == null ? "" : getIntent().getStringExtra("IN_SHARE_DESCRIPTION"));
                postParams.putString("link", getIntent().getStringExtra("IN_SHARE_SHARELINK") == null ? "" : getIntent().getStringExtra("IN_SHARE_SHARELINK"));
                postParams.putString("picture", getIntent().getStringExtra("IN_SHARE_THUMB") == null ? "" : getIntent().getStringExtra("IN_SHARE_THUMB"));
                postParams.putString("message", getIntent().getStringExtra("IN_SHARE_MESSAGE") == null ? "" : getIntent().getStringExtra("IN_SHARE_MESSAGE"));
    
                Request.Callback callback = new Request.Callback() {
    
                    public void onCompleted(Response response) {
                        isSharingData = false;
                        lnrPbr.setVisibility(View.GONE);
                        FacebookRequestError error = response.getError();
                        if (error != null) {
                            IjoomerUtilities.getCustomOkDialog(getString(R.string.facebook_share_title), error.getErrorMessage(), getString(R.string.ok), R.layout.ijoomer_ok_dialog, new CustomAlertNeutral() {
    
                                @Override
                                public void NeutralMethod() {
                                    finish();
                                }
                            });
                        } else {
                            IjoomerUtilities.getCustomOkDialog(getString(R.string.facebook_share_title), getString(R.string.facebook_share_success), getString(R.string.ok), R.layout.ijoomer_ok_dialog, new CustomAlertNeutral() {
    
                                @Override
                                public void NeutralMethod() {
                                    finish();
                                }
                            });
                        }
                    }
                };
    
                Request request = new Request(Session.getActiveSession(), "me/feed", postParams, HttpMethod.POST, callback);
    
                RequestAsyncTask task = new RequestAsyncTask(request);
                task.execute();
            }
    
        }
    

    【讨论】:

      【解决方案3】:

      参考本教程..

      http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/

      public void postToWall() {
          // post on user's wall.
          facebook.dialog(this, "feed", new DialogListener() {
      
              @Override
              public void onFacebookError(FacebookError e) {
              }
      
              @Override
              public void onError(DialogError e) {
              }
      
              @Override
              public void onComplete(Bundle values) {
              }
      
              @Override
              public void onCancel() {
              }
          });
      
      }
      

      【讨论】:

      • 教程和您的答案都使用了折旧的 Facebook 对象。
      • @Hari 我不能使用它,因为它已经被贬值了
      猜你喜欢
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-24
      • 1970-01-01
      • 2015-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多