【问题标题】:Upload Photo to Facebook with HttpClient使用 HttpClient 将照片上传到 Facebook
【发布时间】:2011-05-27 08:08:00
【问题描述】:

我正在尝试使用以下代码通过 Graph API 将照片上传到 Facebook。我不断收到“错误请求”,但不知道为什么。我可以使用具有相同参数的 curl 上传照片。我正在使用带有 HttpClient 的 Java。

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

更新:更多内容。我从回复中获取了更多信息,我得到了这个:

{"error":{"type":"OAuthException","message":"必须使用活动访问令牌来查询当前用户的信息。"}}

但这似乎不对,因为我使用的是 facebook 在授权过程后返回给我的访问令牌。

工作卷曲代码:

curl -F 'access_token=my-access-token' -F 'source=@/path/to/image.jpg' -F 'message=Some caption' https://graph.facebook.com/me/photos

【问题讨论】:

  • 你也可以添加你的工作卷曲代码吗?
  • 添加到帖子底部

标签: java facebook-graph-api apache-commons-httpclient


【解决方案1】:

这是方法的android版本

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

【讨论】:

    【解决方案2】:

    您可以使用 socialauth java api 通过网络应用程序上传图片。

    http://code.google.com/p/socialauth/

    【讨论】:

      【解决方案3】:

      我解决了这个问题。我不需要将参数添加到 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();
          }
      

      【讨论】:

      • 我声明了文件,但它显示错误请求作为响应..........................上传bird.jpg to 'graph.facebook.com/me/photos' 上传失败,response=Bad Request
      • 此主题已有 4.5 年历史。他们的 API 很可能发生了变化,而这不再相关了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2011-04-08
      • 2012-09-20
      相关资源
      最近更新 更多