Facebook API / Android : Wall Post publish with image attachment not working

风流意气都作罢 提交于 2019-12-02 10:22:14

问题


I have the following code.

It works and posts the message-part but the attachment part does'nt work. I suspect it has to do with passing a JSON as a string.

Facebook returns "{"id":"23522646737635675"}. So its not an error.

        Bundle params = new Bundle();

        params.putString("message", message);

        JSONObject attachment = new JSONObject();

        attachment.put("href", URLEncoder.encode("http://a.espncdn.com/photo/2010/0523/pg2_a_cricket_576.jpg"));
        attachment.put("name", "Cricket Fantasy");
        attachment.put("caption", "New team");
        attachment.put("description","Description about Application");

        JSONObject media = new JSONObject();

        media.put("type", "image");
        media.put("src", URLEncoder.encode("http://a.espncdn.com/photo/2010/0523/pg2_a_cricket_576.jpg"));
        media.put("href", URLEncoder.encode("http://a.espncdn.com/photo/2010/0523/pg2_a_cricket_576.jpg"));
        attachment.put("media", media);

        params.putString("attachement", attachment.toString());

        String response = mFacebook.request("me/feed", params, "POST");

回答1:


You can't send json encoded data to facebook, doesn't work that way. Each parameter should be on it's on in the POST body.

In addition, the "attachment" way is an old one and not used anymore. It should look something like:

Bundle params = new Bundle();

params.putString("message", message);
params.put("name", "Cricket Fantasy");
params.put("caption", "New team");
params.put("description","Description about Application");
params.put("url", URLEncoder.encode("http://a.espncdn.com/photo/2010/0523/pg2_a_cricket_576.jpg"));

String response = mFacebook.request("me/feed", params, "POST");

An official reference for uploading images using urls can be found here: Uploading Photos to the Graph API via a URL. The parameters for posting to a feed can be found in the User object doc.




回答2:


Bundle params = new Bundle();
                   // params.putString("multipart/form-data", imgurl);
                    params.putByteArray("multipart/form-data",byteArray);

                    params.putString("caption", txtcaption.getText().toString());
                    /* make the API call */
                    new GraphRequest(
                            AccessToken.getCurrentAccessToken(),
                            "/me/photos",
                            params,
                            HttpMethod.POST,
                            new GraphRequest.Callback() {
                                public void onCompleted(GraphResponse response) {
                                    /* handle the result */
                                    Log.e("responseImagedata---", response.toString());

                                }
                            }
                    ).executeAsync();


来源:https://stackoverflow.com/questions/10510071/facebook-api-android-wall-post-publish-with-image-attachment-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!