0

我正在使用 facebook sdk 向 facebook 的“what in your mind box”发布一个 URL,但是当我查看 facebook 帐户时,没有任何内容被发布。有人可以告诉我如何使用 facebook sdk 使用标题将 url 发布到 facebook 墙上吗?

/**
     * Function to login into facebook
     * */
    public void loginToFacebook() {

        mPrefs = getPreferences(MODE_PRIVATE);
        String access_token = mPrefs.getString("access_token", null);
        long expires = mPrefs.getLong("access_expires", 0);

        if (access_token != null) {
            Constants.facebook.setAccessToken(access_token);

            /*
            btnFbLogin.setVisibility(View.INVISIBLE);

            // Making get profile button visible
            btnFbGetProfile.setVisibility(View.VISIBLE);

            // Making post to wall visible
            btnPostToWall.setVisibility(View.VISIBLE);

            // Making show access tokens button visible
            btnShowAccessTokens.setVisibility(View.VISIBLE);
                */
            Log.d("FB Sessions", "" + Constants.facebook.isSessionValid());

        }

        if (expires != 0) {
            Constants.facebook.setAccessExpires(expires);
        }

        if (!Constants.facebook.isSessionValid()) {
            Constants.facebook.authorize(this,
                    new String[] { "email", "publish_stream" },
                    new DialogListener() {

                        @Override
                        public void onCancel() {
                            // Function to handle cancel event
                        }

                        @Override
                        public void onComplete(Bundle values) {
                            // Function to handle complete event
                            // Edit Preferences and update facebook acess_token
                            SharedPreferences.Editor editor = mPrefs.edit();
                            editor.putString("access_token",
                                    Constants.facebook.getAccessToken());
                            editor.putLong("access_expires",
                                    Constants.facebook.getAccessExpires());
                            editor.commit();

                            // Making Login button invisible
                            /*btnFbLogin.setVisibility(View.INVISIBLE);

                            // Making logout Button visible
                            btnFbGetProfile.setVisibility(View.VISIBLE);

                            // Making post to wall visible
                            btnPostToWall.setVisibility(View.VISIBLE);

                            // Making show access tokens button visible
                            btnShowAccessTokens.setVisibility(View.VISIBLE);
                        */}

                        @Override
                        public void onError(DialogError error) {
                            // Function to handle error

                        }

                        @Override
                        public void onFacebookError(FacebookError fberror) {
                            // Function to handle Facebook errors

                        }

                    });
        }
    }

postonWall(" http://google.com/ ");

public void postOnWall(String msg) {
     try {
            String response = facebook.request("me");
            Bundle parameters = new Bundle();
            parameters.putString("caption",url);   

           // parameters.putString("caption", "Test Caption: http://google.com/");
           // parameters.putString("method", "stream.publish");
            response = facebook.request("me/feed", parameters, 
                    "POST");
            Log.d("Tests", "got response: " + response);
            if (response == null || response.equals("") || 
                    response.equals("false")) {
               Log.v("Error", "Blank response");
            }
     } catch(Exception e) {
         e.printStackTrace();
     }
}
4

1 回答 1

2

我检查您的代码,确保您的会话有效。异常抛出:“缺少消息或附件”。因此,请同时传递“消息”,请求如下所示。

public void postOnWall(String msg) {
     try {
            String response = facebook.request("me");
            Bundle parameters = new Bundle();
            parameters.putString("caption",msg);
            parameters.putString("message","message1");
            response = facebook.request("me/feed", parameters, 
                    "POST");
            Log.d("System out", "got response: " + response);
            if (response == null || response.equals("") || 
                    response.equals("false")) {
               Log.e("System out", "Blank response");
            }
     } catch(Exception e) {
         e.printStackTrace();
     }
}

查看更多参数

希望它可以帮助你:)

于 2013-07-16T09:13:57.970 回答