【问题标题】:Post image from android to facebook page将图像从 android 发布到 facebook 页面
【发布时间】:2015-04-21 19:22:13
【问题描述】:

我已成功在 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();

但是,我无法将图片发布到 Facebook 页面。问题是我无法在 Graph Api 中发布的 Json 数据中找到图像附件的密钥。

来自 facebook 的失败响应是

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

【问题讨论】:

    标签: android facebook-graph-api facebook-sdk-4.0


    【解决方案1】:

    最后,终于,我能够将图片发布到 Facebook 页面。这就是我发布照片的方式。

      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();
    

    【讨论】:

    • 这太棒了,tnx 人 :)。你能告诉我你是如何检索现有专辑的 page_id 的吗?
    【解决方案2】:

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

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

    将照片发布到 Facebook 有两种不同的方式:

    1:将照片附加为 multipart/form-data。对象的名称 没关系,但历史上人们一直使用源作为 照片的参数名称。其工作原理取决于您使用的 SDK 恰好是用来发帖的。

    2:使用已在 Internet 上的照片,通过使用 url参数:

    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();
    

    无法在同一个图形 API 中发布多于一张照片 打电话。

    3.) 示例 ==>

    像这样尝试,即发布照片的 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();
    

    【讨论】:

    猜你喜欢
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多