使用 Google API Java 客户端上传到 YouTube 时出现问题
我正在使用 Google API Java 客户端库 (http://code.google.com/p/google-api-java-client/) 将 Android 手机上的视频上传到 YouTube。
这是我到目前为止的代码:
// Create the meta data
Entry entry = new Entry();
entry.group = new MediaGroup();
entry.group.title = "test title";
entry.group.description = "test description";
entry.group.category = new MediaCategory();
entry.group.category.Cat = "People\n";
XmlNamespaceDictionary namespaceDictionary = Util.NAMESPACE_DICTIONARY;
AtomContent aContent = new AtomContent();
aContent.entry = entry;
aContent.namespaceDictionary = namespaceDictionary;
// Create the actual video data
InputStreamContent bContent = new InputStreamContent();
bContent.inputStream =
context.getContentResolver().openInputStream(media);
bContent.type = context.getContentResolver().getType(media);
bContent.length = videoAsset.getLength();
// Build up the POST request
MultipartRelatedContent multiContent = new MultipartRelatedContent();
multiContent.parts.add(aContent);
multiContent.parts.add(bContent);
HttpRequest request = transport.buildPostRequest();
request.url = YouTubeUrl.uploadUrl();
GoogleHeaders headers = (GoogleHeaders) request.headers;
headers.setSlugFromFileName("mytest.3gpp");
request.content = multiContent;
request.headers.contentType = multiContent.getType();
request.execute();
不幸的是,我在调用 request.execute() 时遇到异常:
03-06 23:28:58.887: WARN/System.err(18923): com.google.api.client.http.HttpResponseException: 415 Unsupported Media Type
03-06 23:28:58.897: WARN/System.err(18923): at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:221)
03-06 23:28:58.897: WARN/System.err(18923): at com.abless.vidshare.YouTubeAPI.upload(YouTubeAPI.java:118)
03-06 23:28:58.897: WARN/System.err(18923): at com.abless.vidshare.VidShare.onActivityResult(VidShare.java:185)
03-06 23:28:58.897: WARN/System.err(18923): at android.app.Activity.dispatchActivityResult(Activity.java:3931)
03-06 23:28:58.907: WARN/System.err(18923): at android.app.ActivityThread.deliverResults(ActivityThread.java:3730)
03-06 23:28:58.907: WARN/System.err(18923): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776)
03-06 23:28:58.907: WARN/System.err(18923): at android.app.ActivityThread.access$2800(ActivityThread.java:135)
03-06 23:28:58.907: WARN/System.err(18923): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2166)
03-06 23:28:58.907: WARN/System.err(18923): at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 23:28:58.907: WARN/System.err(18923): at android.os.Looper.loop(Looper.java:144)
03-06 23:28:58.907: WARN/System.err(18923): at android.app.ActivityThread.main(ActivityThread.java:4937)
03-06 23:28:58.907: WARN/System.err(18923): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 23:28:58.917: WARN/System.err(18923): at java.lang.reflect.Method.invoke(Method.java:521)
03-06 23:28:58.917: WARN/System.err(18923): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-06 23:28:58.917: WARN/System.err(18923): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-06 23:28:58.917: WARN/System.err(18923): at dalvik.system.NativeStart.main(Native Method)
我做错了什么? MultipartRelatedContent 似乎没有自动指定 Content-Length,这可能是问题所在吗?我看了一下实现代码,MultipartRelatedContent.getLength() 返回-1,这不是很有帮助。
有人成功使用 Google API Java 客户端库将视频上传到 YouTube 吗?
谢谢。
-Alexander
编辑:即使我填写了内容长度部分,我仍然收到相同的错误(415 不支持的媒体类型),所以这并不能解决问题。看起来好像 POST 标头是错误的?
I am using the Google API Java Client library (http://code.google.com/p/google-api-java-client/) to upload a video on my Android phone to YouTube.
Here's the code I have so far:
// Create the meta data
Entry entry = new Entry();
entry.group = new MediaGroup();
entry.group.title = "test title";
entry.group.description = "test description";
entry.group.category = new MediaCategory();
entry.group.category.Cat = "People\n";
XmlNamespaceDictionary namespaceDictionary = Util.NAMESPACE_DICTIONARY;
AtomContent aContent = new AtomContent();
aContent.entry = entry;
aContent.namespaceDictionary = namespaceDictionary;
// Create the actual video data
InputStreamContent bContent = new InputStreamContent();
bContent.inputStream =
context.getContentResolver().openInputStream(media);
bContent.type = context.getContentResolver().getType(media);
bContent.length = videoAsset.getLength();
// Build up the POST request
MultipartRelatedContent multiContent = new MultipartRelatedContent();
multiContent.parts.add(aContent);
multiContent.parts.add(bContent);
HttpRequest request = transport.buildPostRequest();
request.url = YouTubeUrl.uploadUrl();
GoogleHeaders headers = (GoogleHeaders) request.headers;
headers.setSlugFromFileName("mytest.3gpp");
request.content = multiContent;
request.headers.contentType = multiContent.getType();
request.execute();
Unfortunately, I get an exception when calling request.execute():
03-06 23:28:58.887: WARN/System.err(18923): com.google.api.client.http.HttpResponseException: 415 Unsupported Media Type
03-06 23:28:58.897: WARN/System.err(18923): at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:221)
03-06 23:28:58.897: WARN/System.err(18923): at com.abless.vidshare.YouTubeAPI.upload(YouTubeAPI.java:118)
03-06 23:28:58.897: WARN/System.err(18923): at com.abless.vidshare.VidShare.onActivityResult(VidShare.java:185)
03-06 23:28:58.897: WARN/System.err(18923): at android.app.Activity.dispatchActivityResult(Activity.java:3931)
03-06 23:28:58.907: WARN/System.err(18923): at android.app.ActivityThread.deliverResults(ActivityThread.java:3730)
03-06 23:28:58.907: WARN/System.err(18923): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776)
03-06 23:28:58.907: WARN/System.err(18923): at android.app.ActivityThread.access$2800(ActivityThread.java:135)
03-06 23:28:58.907: WARN/System.err(18923): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2166)
03-06 23:28:58.907: WARN/System.err(18923): at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 23:28:58.907: WARN/System.err(18923): at android.os.Looper.loop(Looper.java:144)
03-06 23:28:58.907: WARN/System.err(18923): at android.app.ActivityThread.main(ActivityThread.java:4937)
03-06 23:28:58.907: WARN/System.err(18923): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 23:28:58.917: WARN/System.err(18923): at java.lang.reflect.Method.invoke(Method.java:521)
03-06 23:28:58.917: WARN/System.err(18923): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-06 23:28:58.917: WARN/System.err(18923): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-06 23:28:58.917: WARN/System.err(18923): at dalvik.system.NativeStart.main(Native Method)
What am I doing wrong? It seems like Content-Length is not automatically specified by MultipartRelatedContent, might that be the problem? I had a look at the implementation code, and MultipartRelatedContent.getLength() returns -1, which is not very helpful.
Has anyone managed to use the Google API Java Client library to successfully upload a video to YouTube?
Thanks.
-Alexander
Edit: even if I fill out the Content-Length part, I still get the same error (415 Unsupported Media Type), so that doesn't solve it. It almost seems like the POST header is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我自己弄清楚了...原来 YouTubeURL 使用了 JsonC 选项。注释掉该行后,一切都很顺利。
Figured it out myself...turns out the YouTubeURL used the JsonC option. After commenting out that line, everything worked beautifully.
如果有人对使用新的 google-api-java-client 的示例感兴趣,我已将其附加到 错误 16。
If someone is interested in an example using the new google-api-java-client, I have attached one to bug 16.