尝试发布到朋友墙时OAuthException

时间:2012-12-11 21:50:58

标签: android facebook

我尝试使用Android中的Facebook api发布到朋友的墙上有很多麻烦。这就是我现在所拥有的:

if (facebook.isSessionValid()) {
                        String response = facebook.request((userID == null) ? "me" : userID);

                        Bundle params = new Bundle();
                        params.putString("message", "put message here");
                        params.putString("link", "http://mylink.com");    
                        params.putString("caption", "{*actor*} just posted this!");
                        params.putString("description", "description of my link.  Click the link to find out more.");
                        params.putString("name", "Name of this link!");
                        params.putString("picture", "http://mysite.com/picture.jpg");

                        response = facebook.request(((userID == null) ? "me" : userID) + "/feed", params, "POST");       

                        Log.d("Tests",response);
                        if (response == null || response.equals("") || 
                                response.equals("false")) {
                            Log.v("Error", "Blank response");
                        }
                    } else {
                        // no logged in, so relogin
                        Log.d("1234567890", "sessionNOTValid, relogin");

                    }
                }catch(Exception e){
                    e.printStackTrace();
                }

但是这会返回错误:

12-11 21:34:06.604: D/FACEBOOK RESPONSE(14954): {"error":{"message":"(#200) Feed story publishing to other users is disabled for this application","type":"OAuthException","code":200}}

2 个答案:

答案 0 :(得分:15)

您最近可能创建了这个Facebook应用程序,这意味着2013年2月的重大更改已启用。

February's Breaking Changes包括:

  

删除通过图谱API发布到朋友墙的功能

     

我们将删除通过Graph API发布到用户朋友的墙上的功能。具体来说,针对[user_id] / feed的帖子,其中[user_id]与会话用户不同,或者stream_publish调用,其中target_id用户与会话用户不同,将失败。如果您想允许人们发布到他们朋友的时间轴,请调用Feed对话框。通过用户提及标记或动作标记包含朋友的故事将显示在朋友的时间线上(假设朋友批准标记)。有关详细信息,请参阅此博客文章。

如果您希望启用此功能,我们将从2月开始停用此功能(仅暂时启用至2月),请转到您的应用信息中心>设置>高级>禁用“2013年2月重大变化”

我强烈建议不要这样做,但是,自2月开始此功能将导致您的应用再次抛出相同的错误。

答案 1 :(得分:2)

我有一个可能对您有所帮助的解决方案,我正在使用它来处理我的代码并且工作正常......

private void publishFeedDialog(String friend_uid){

    try{
            Session mCurrentSession = Session.getActiveSession();

            SessionTracker mSessionTracker = new SessionTracker(
                    getBaseContext(), new StatusCallback() {
                        public void call(Session session, SessionState state,
                                Exception exception) {
                        }
                    }, null, false);
            String applicationId = Utility
            .getMetadataApplicationId(getBaseContext());
            mCurrentSession = mSessionTracker.getSession();

            if (mCurrentSession == null
                    || mCurrentSession.getState().isClosed()) {
                mSessionTracker.setSession(null);
                Session session = new Session.Builder(getBaseContext())
                .setApplicationId(applicationId).build();
                Session.setActiveSession(session);
                mCurrentSession = session;
            }

            if (!mCurrentSession.isOpened()) {
                Session.OpenRequest openRequest = null;
                openRequest = new Session.OpenRequest(
                        NewFriendList.this);

                if (openRequest != null) {
                    openRequest
                    .setDefaultAudience(SessionDefaultAudience.FRIENDS);
                    openRequest.setPermissions(Arrays.asList("email", "publish_actions"));
                    openRequest
                    .setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);



                    mCurrentSession.openForPublish(openRequest);
                }
            }



            if (regobj != null && friend_uid != null ) {

                final Activity activity = this;
                Bundle params = new Bundle();
                //This is what you need to post to a friend's wall
                params.putString("from", "" + regobj.MyFBID);
                params.putString("to", friend_uid);
                //up to this
                params.putString("name", "Facebook SDK for Android");
                params.putString("caption", "Build great social apps and get more installs.");
                params.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
                params.putString("link", "https://developers.facebook.com/android");
                params.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");
                WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(this, mCurrentSession, params))
                        .setOnCompleteListener(new OnCompleteListener() {

                        @Override
                        public void onComplete(Bundle values, FacebookException error) {
                            if (error == null) {
                                // When the story is posted, echo the success
                                // and the post Id.
                                final String postId = values.getString("post_id");
                                if (postId != null) {
                                    Toast.makeText(activity,
                                        "Posted story, id: "+postId,
                                        Toast.LENGTH_SHORT).show();
                                } else {
                                    // User clicked the Cancel button
                                    Toast.makeText(activity, 
                                        "Publish cancelled", 
                                        Toast.LENGTH_SHORT).show();
                                }
                            } else if (error instanceof FacebookOperationCanceledException) {
                                // User clicked the "x" button
                                Toast.makeText(activity, 
                                    "Publish cancelled", 
                                    Toast.LENGTH_SHORT).show();
                            } else {
                                // Generic, ex: network error
                                Toast.makeText(activity, 
                                    "Error posting story", 
                                    Toast.LENGTH_SHORT).show();
                            }
                        }



                    }).build();
                feedDialog.show();
            }
    }catch(Exception e)
    {
        Log.d("Error", ""+e.toString());
    }
}

此代码仅适用于一个用户,如果要将其发送给多个用户,则可以使用RequestsDialogBu​​ilder而不是WebDialog。