使用 HttpClient 将照片上传到 Facebook
我正在尝试使用以下代码通过 Graph API 将照片上传到 Facebook。我不断收到“错误请求”,但不知道为什么。我可以使用具有相同参数的curl 很好地上传照片。我正在使用 Java 和 HttpClient。
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
I am attempting to use the following code to upload a photo to Facebook using the Graph API. I keep getting "Bad Request" but not sure why. I can upload the photo just fine using curl with the same parameters. I'm using Java with HttpClient.
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. I grabbed some more info out the response and I am getting this:
{"error":{"type":"OAuthException","message":"An active access token must be used to query information about the current user."}}
But that doesn't seem right as I'm using the access token that facebook gives back to me after the authorize process.
Working curl code:
curl -F 'access_token=my-access-token' -F 'source=@/path/to/image.jpg' -F 'message=Some caption' https://graph.facebook.com/me/photos
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我解决了这个问题。我不需要将参数添加到 PostMethod,而是需要将 access_token 和消息添加到 Part[] 数组。完整代码:
I solved the problem. Instead of adding the params to the PostMethod, I needed to add the access_token and message to the Part[] array. Full code:
您可以使用socialauth java api通过Web应用程序上传图像。
http://code.google.com/p/socialauth/
You can use the socialauth java api for uploading image through web application.
http://code.google.com/p/socialauth/
这是方法的android版本
this is the android version of method