使用Google API Java客户端上传到YouTube时出现问题 [英] Problem uploading to YouTube using Google API Java Client

查看:259
本文介绍了使用Google API Java客户端上传到YouTube时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google API Java客户端库(http://code.google.com/p/google-api-java-client/)将Android手机上的视频上传到YouTube。

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();

不幸的是,我在调用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)

我做错了什么?看起来Content-Length不是由MultipartRelatedContent自动指定的,这可能是问题吗?我查看了实现代码,并且MultipartRelatedContent.getLength()返回-1,这不是很有用。

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.

有没有人设法使用Google API Java客户端库成功将视频上传到YouTube?

Has anyone managed to use the Google API Java Client library to successfully upload a video to YouTube?

谢谢。

-Alexander

-Alexander

编辑:即使我填写了Content-Length部分,仍然会得到相同的错误(415 Unsupported Media Type),所以这并不能解决问题。它几乎看起来像POST头是错的?

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?

推荐答案

如果有人对使用新的google-api-java -client,我附加了一个 bug 16

If someone is interested in an example using the new google-api-java-client, I have attached one to bug 16.

这篇关于使用Google API Java客户端上传到YouTube时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆