问题描述
我想将照片(存储在appengine数据库中)发布到Facebook。
I want to post a photo (stored in appengine db) to facebook.
要测试我已经掌握了本地的基本了解: strong>成功:
To test I've got the basic understanding down locally: I've been successful with this form:
<form action="https://graph.facebook.com/7378294228/photos?access_token=AAAAAJPBSAzcBALmz7GOLZCER7Pc2347WQIDIlIFR8e2imWUzeuCKRLrXjAqR6zjaUb4laqkLtJlQlYa7X5ZBd2aNJoLom8M7IlvHfw39QZDZD" method="POST" enctype="multipart/form-data">
<input type="file" name="source" id="source"/>
<input type="text" name="message" value="mymess"/>
<input type="Submit"/>
</form>
(我从最近的一个会话中抓住了access_token,使其工作。)
(I grabbed the access_token from a recent session to make this work.)
以下是我迄今为止在appengine 失败上尝试过的
Here's what I've tried on appengine unsuccessfully so far:
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new ByteArrayBody(imageBytes, "image/jpeg", "w.jpg");
mpEntity.addPart("source", cbFile);
URL url = new URL("https://graph.facebook.com/"+albumUpload.getAlbumID()+"/photos?access_token="+albumUpload.getAuthToken());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
mpEntity.writeTo(connection.getOutputStream());
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.err.println("http success!");
}else{
System.err.println("http failed:"+connection.getResponseCode());
}
我收到一个HTTP 400 - 错误请求。
I get a HTTP 400 - Bad Request.
我添加了这些,以确保它在做某事:
I added these to make sure it was doing something:
System.out.println("mpEntity image content length: "+cbFile.getContentLength());
System.out.println("mpEntity content type:"+mpEntity.getContentType());
导致:
mpEntity image content length: 786145
mpEntity content type:Content-Type: multipart/form-data; boundary=oMiJCBHGVvZmU7s3FcUGXMbyU23aX_Ow
我可以在线查看MultipartEntity使用的唯一示例是使用HttpClient的setEntity()作为因此不适用,因为这是appengine下的URLFetch。
The only examples I can find of MultipartEntity usage online are using HttpClient's setEntity(), as as such don't apply as this is a URLFetch under appengine.
感谢任何帮助/代码。
推荐答案
解决!
我需要添加:
connection.addRequestProperty("Content-length", mpEntity.getContentLength()+"");
connection.addRequestProperty(mpEntity.getContentType().getName(), mpEntity.getContentType().getValue());
另外更改:
MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
希望这有助于某人
这篇关于GAE / J:如何从appengine到Facebook发布多部分MIME消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!