我正在尝试使用以下代码使用 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