使用HttpClient将照片上传到Facebook [英] Upload Photo to Facebook with HttpClient
问题描述
我正在尝试使用以下代码使用Graph API将照片上传到Facebook。我一直收到错误请求,但不确定原因。我可以使用相同参数的curl上传照片。我正在使用Java和HttpClient。
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:有效访问令牌必须用于查询有关当前用户的信息。}}
{"error":{"type":"OAuthException","message":"An active access token must be used to query information about the current user."}}
但这似乎不对,因为我正在使用facebook发回的访问令牌授权过程后给我。
But that doesn't seem right as I'm using the access token that facebook gives back to me after the authorize process.
工作卷曲代码:
curl -F 'access_token=my-access-token' -F 'source=@/path/to/image.jpg' -F 'message=Some caption' https://graph.facebook.com/me/photos
推荐答案
我解决了这个问题。我没有将params添加到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:
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();
}
这篇关于使用HttpClient将照片上传到Facebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!