2015-04-19 57 views
0

我已经成功地在facebook页面中的图表api中发布了订阅源。从Android到Facebook页面的图像

 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页面。问题是我无法找到图片Api中发布的Json数据中的图像附件的关键。

从Facebook失败的反应是

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

回答

2

最后的这即岗位byteArrayStream,最后,我还是能够将图像上传到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(); 
+0

这是伟大的,TNX人:)。你能告诉我你是如何检索现有专辑的page_id的? – DoubleK

0

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

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

有发布照片至Facebook的两个独立的途径:

1:附加照片作为多部分/形式的数据。对象 的名称并不重要,但历史上人们已将源用作照片的参数名称 。这是如何工作的,取决于您正在使用的帖子发布的 的SDK。

2:使用照片是已经在互联网上使用 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)实例==>

试着像您的照片

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

Facebook发布了新的sdk v4,其中v3中的Request和Response类现在称为GraphRequest和GraphResponse。请求类不工作。 https://developers.facebook.com/docs/android/upgrading-4.x – user3308752