Java 使用scribe库将图像发布到Facebook页面
我正在尝试使用图书馆向facebook发布一张图片。 我的代码是:Java 使用scribe库将图像发布到Facebook页面,java,facebook,scribe,Java,Facebook,Scribe,我正在尝试使用图书馆向facebook发布一张图片。 我的代码是: String apiKey = "MY_API_KEY"; String apiSecret = "MY_SECRET_KEY"; OAuthService service = new ServiceBuilder().provider(FacebookApi.class).apiKey(apiKey) .apiSecret(apiSecret)
String apiKey = "MY_API_KEY";
String apiSecret = "MY_SECRET_KEY";
OAuthService service = new ServiceBuilder().provider(FacebookApi.class).apiKey(apiKey)
.apiSecret(apiSecret)
.callback("MY_CALL_BACK_URL")
.build();
Token accessToken = new Token("MY_ACCESS_TOKEN","");
OAuthRequest request = new OAuthRequest(Verb.POST, PROTECTED_RESOURCE_URL_STREAM);
request.addHeader("Content-Type", "text/html");
request.addBodyParameter("message", "Testing auto update.. Please ignore " + new DateTime());
//MultiPart for pic
Multipart mp = new MimeMultipart();
MimeBodyPart htmlPart = new MimeBodyPart();
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
byte[] imagePayLoad = msgBody.getBytes("UTF-8"); //msgBody contains html string
byte[] mpByte = new byte[imagePayLoad.length];
htmlPart.setContent(msgBody, "text/html");
mp.addBodyPart(htmlPart);
//here I get the bytes[] of mp to out
mp.writeTo(out);
out.write(mpByte);
request.addPayload(mpByte);
} catch (Exception e) {
e.printStackTrace();
}
service.signRequest(accessToken, request);
Response response = request.send();
System.out.println("Response");
System.out.println();
System.out.println(response.getCode());
System.out.println(response.getBody());
}
但我也有
400
{"error":{"message":"(#100) Missing message or attachment","type":"OAuthException","code":100}}
如果我拿出多部分,文本“测试自动更新..请我..”将更新到Facebook,请任何人帮忙。我需要上传一张图片和一个字节[]
谢谢
我正在使用:
<dependency>
<groupId>org.scribe</groupId>
<artifactId>scribe</artifactId>
<version>1.3.0</version>
</dependency>
org.scribe
抄写员
1.3.0
Scribe使用addBodyParameter
或addPayload
,而不是两者都使用。看
如果您使用addPayload
方法,那么我想您也必须弄清楚如何将消息
放入多部分
注意:请求的实际内容的字符串输出(使用)会很好。注意,最新版本是
1.3.1
(这是不相关的,但在最新的稳定版本上总是很酷)。感谢@Pablo正要问我在我的库中没有看到一些方法和org.scribe.model.request,我刚刚看到了你关于1.3.1版的更新。我还注意到字节[]getByteBodyContent()不可访问,事实上Eclipse警告我org.scribe.model.Request不可见。感谢您的帮助:)getBodyContents
应该是公共的(请参阅链接)。它是返回字符串的变量。由于我发送的是二进制内容,所以我对getByteBodyContents()
感兴趣,它不是公共的,如org.scribe.model.Request
第256行所示。它没有修改器,因此无法从org.scribe.model.OAuthRequest
访问-这是设计的吗?我能把回购和修复分开吗ThanksI已经能够验证我作为字节[]发送的请求,但我仍然收到400个错误。已发送请求:[B@772a7557响应400{“error”:{“message”:(#100)缺少消息或附件”,“type”:“OAutheException”,“code”:100}
有关更多信息,我确实需要查看带有getBodyContents
的多部分消息。分叉lib以获取字节是一种过分的做法,因为您可以轻松地将字符串转换为字节数组。