2010-12-17 95 views
4

我正尝试使用以下代码将图片上传到Facebook使用Graph API。我不断收到“不良要求”,但不知道为什么。我可以用相同的参数使用curl来上传照片。我用HttpClient使用Java。使用HttpClient将照片上传至Facebook

PostMethod filePost = new PostMethod('https://graph.facebook.com/me/photos'); 
    filePost.setParameter('access_token', 'my-access-token') 
    filePost.setParameter('message', 'test image') 

    filePost.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false); 
    try { 
     println("Uploading " + file.getName() + " to 'https://graph.facebook.com/me/photos'"); 
     Part[] parts = [new FilePart('source', file.getName(), file)] 
     filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams())); 
     HttpClient client = new HttpClient(); 
     client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); 
     int status = client.executeMethod(filePost); 
     if (status == HttpStatus.SC_OK) { 
     println(
       "Upload complete, response=" + filePost.getResponseBodyAsString() 
     ); 
     } else { 
     println(
       "Upload failed, response=" + HttpStatus.getStatusText(status) 
     ); 
     } 
    } catch (Exception ex) { 
     println("ERROR: " + ex.getClass().getName() + " " + ex.getMessage()); 
     ex.printStackTrace(); 
    } finally { 
     filePost.releaseConnection(); 
    } 

UPDATE:More to this。我抓住了一些更多的信息,我得到了这个:

{“error”:{“type”:“OAuthException”,“message”:“一个活跃的访问令牌必须用来查询有关当前的信息用户。“}}

但这看起来不正确,因为我使用Facebook在授权过程后给予我的访问令牌。

工作卷曲代码:

curl -F 'access_token=my-access-token' -F '[email protected]/path/to/image.jpg' -F 'message=Some caption' https://graph.facebook.com/me/photos 
+0

您可以将您的工作卷曲代码太Android版本? – 2010-12-17 22:37:20

+0

添加到帖子底部 – Gregg 2010-12-17 22:42:15

回答

4

我解决了这个问题。我不需要将参数添加到PostMethod,而是需要将access_token和消息添加到Part []数组中。完整代码:

PostMethod filePost = new PostMethod('https://graph.facebook.com/me/photos'); 
    filePost.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false); 
    try { 
     println("Uploading " + file.getName() + " to 'https://graph.facebook.com/me/photos'"); 
     Part[] parts = [new FilePart('source', file.getName(), file), new StringPart('access_token', "${facebookData.access_token}"), new StringPart('message', 'some message')] 
     filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams())); 
     HttpClient client = new HttpClient(); 
     client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); 
     int status = client.executeMethod(filePost); 
     if (status == HttpStatus.SC_OK) { 
     println("Upload complete, response=" + filePost.getResponseBodyAsString()); 
     } else { 
     println("Upload failed, response=" + HttpStatus.getStatusText(status)); 
     // Create response 
     StringBuilder notificationsSendResponse = new StringBuilder(); 
     byte[] byteArrayNotifications = new byte[4096]; 
     for (int n; (n = filePost.getResponseBodyAsStream().read(byteArrayNotifications)) != -1;) { 
      notificationsSendResponse.append(new String(byteArrayNotifications, 0, n)); 
     } 
     String notificationInfo = notificationsSendResponse.toString(); 
     } 
    } catch (Exception ex) { 
     println("ERROR: " + ex.getClass().getName() + " " + ex.getMessage()); 
     ex.printStackTrace(); 
    } finally { 
     filePost.releaseConnection(); 
    } 
+0

我需要一些帮助..你在哪里声明“文件”参数? – chopss 2015-04-29 06:52:12

+0

我声明了文件,但它显示不好的请求作为回应............................上传bird.jpg到'https://图.facebook.com/me/photos' 上传失败,回复=错误请求 – chopss 2015-04-29 09:27:12

+0

此主题已有4.5年历史。他们的API很可能已经改变,并且这已经不再相关。 – Gregg 2015-04-29 19:27:49

1

这是方法

private void postOnFacebook() { 
     try { 
      HttpPost httpPost = new HttpPost("https://graph.facebook.com/me/photos"); 
      MultipartEntity entity = new MultipartEntity(); 
      String base64Image = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAASSURBVBhXY3gro4KMKOPLqAAAq/UdZuRmLacAAAAASUVORK5CYII="; 
      byte[] imageData = Base64.decode(base64Image, 0); 
      entity.addPart("access_token", new StringBody("your access token")); 
      entity.addPart("message", new StringBody("test msg")); 
      entity.addPart("source", new ByteArrayBody(imageData, "test")); 
      CloseableHttpClient httpclient = HttpClientBuilder.create().build(); 
      httpPost.getParams().setBooleanParameter(USE_EXPECT_CONTINUE, false); 
      httpPost.setEntity(entity); 
      HttpResponse resp = httpclient.execute(httpPost); 
      HttpEntity entity2 = resp.getEntity(); 
      if (entity != null) { 
       String responseBody = EntityUtils.toString(entity2); 
       responseBody.toString(); 
      } 
     } catch (Exception ex) { 

      ex.printStackTrace(); 
     } 
    }