Post 图片来自 android 到 Facebook 页面

Post image from android to facebook page

我已成功 post 在 facebook 页面中形成图表 api。

     try {
                   resObj.put("message","feed from android");
//resObj.put("object_attachment",bitmap);

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

                GraphRequest request = GraphRequest.newPostRequest(
                        AccessToken.getCurrentAccessToken(),"363453267193844/photos",resObj,
                        new GraphRequest.Callback() {
                            @Override
                            public void onCompleted(GraphResponse graphResponse) {
                                Log.i(TAG,"post page response::"+graphResponse);

                            }
                        }
                    );

                request.executeAsync();

但是,我无法 post 将图像放入 Facebook 页面。问题是我无法在图表 Api.

中的 Json 数据 post 中找到图像附件的密钥

Facebook 的失败响应是

{Response:  responseCode: 400, graphObject: null, error: {HttpStatus: 400, errorCode: 324, errorType: OAuthException, errorMessage: (#324) Requires upload file}}

1.) 确保您的页面访问令牌具有 publish_pages 权限,可用于发布新照片。

2.) 来自 docs 。请注意,您在调用中的 pageid 之前没有“/”。

There are two separate ways of publishing photos to Facebook:

1: Attach the photo as multipart/form-data. The name of the object doesn't matter, but historically people have used source as the parameter name for the photo. How this works depends on the SDK you happen to be using to do the post.

2: Use a photo that is already on the internet by publishing using the url parameter:

Bundle params = new Bundle();
params.putString("url", "{image-url}");
/* make the API call */
new Request(
    session,
    "/{page-id}/photos",
    params,
    HttpMethod.POST,
    new Request.Callback() {
        public void onCompleted(Response response) {
            /* handle the result */
        }
    }
).executeAsync();

There is no way to publish more then one photo in the same graph API call.

3.) 示例 ==>

像这样尝试,即 post 你照片的 byteArrayStream

postParams = new Bundle();

            postParams.putString("message", "feed from android");
            postParams.putBoolean("published",true);
            String pageID = "363453267193844";

            //Post to the page as the page user
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            <YOURBITMAPPHOTOHANDLE>.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();
            postParams.putByteArray("source", byteArray);

            postParams.putString("access_token", "your_page_access_token");

         /* make the API call */
            new Request(
                    sessionInstance,
                    "/" + pageID + "/photos",
                    postParams,
                    HttpMethod.POST,
                    new Request.Callback() {
                        public void onCompleted(Response response) {
                            //An error occurred during posting to facebook
                            FacebookRequestError error = response.getError();
                            if (error != null) {
                                isPostingError = true;
                                postingErrorMessage = error.getErrorUserMessage();

                            } else {
                                isPostingError = false;
                            }

                        }


                    }
            ). executeAsync();

终于,终于,我能够post一张图片进入facebook页面了。这就是我 post 一张照片的处理方式。

  Bundle bundle=new Bundle();
        bundle.putByteArray("object_attachment",byteArray);// object attachment must be either byteArray or bitmap image
        bundle.putString("message","some message here");

        GraphRequest graphRequest=new GraphRequest(AccessToken.getCurrentAccessToken(),
                "{page_id}/photos",
                bundle,
                HttpMethod.POST,
                new GraphRequest.Callback() {

                    @Override
                    public void onCompleted(GraphResponse graphResponse) {

                      Log.i("post page response::" + graphResponse);

                }
        );
        graphRequest.executeAsync();