YouTube API 集成 ASP.NET - 单页上传视频 [英] YouTube API integration ASP.NET - upload video with a single page

查看:15
本文介绍了YouTube API 集成 ASP.NET - 单页上传视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试使用 Google YouTube API 通过浏览器直接将视频从我们的网站上传到 YouTube.

we are trying to use the Google YouTube API to upload videos from our website to YouTube through the browser directly.

API 分两步工作,第一步我们需要创建一个包含所有元数据(如标题、标签、描述、类别等)的 Video 对象.然后我们需要向 YouTube 发送一个包含此对象的请求并获取一个作为响应生成的令牌对象.这个令牌对象有一个令牌值和一个令牌 URL 作为它的成员.

The API works in two steps, in the first step we need to create an Video object with all the metadata like title,tags,description, category etc. Then we need to send a request with this object to YouTube and get a Token object generated as a response. This token object has a Token Value and a Token URL as its members.

在第二步中,他们建议我们应该创建一个将 action 属性设置为 Token URL 的表单.这个表单应该有一个文件上传控件和一个以 Token Value 作为其值的隐藏字段.发布此表单后,它会将视频上传到 YouTube.

In second step they suggest that we should create a Form with the action attribute set to the Token URL. This form should have a file upload control and a hidden field with the Token Value as its value. When this form is posted, it would upload the video to YouTube.

如果我们将其分为两步流程,即首先向用户询问元数据,然后将他们重定向到第二页以进行实际视频上传,这将非常有效.但是,我们试图在一个页面中完成此操作,向用户显示为一个步骤.

This works great if we have it as a two step process, asking users for the metadata first and then redirecting them on to second page for the actual video upload. However, we are trying to accomplish this in a single page, appearing as a single step to the user.

我们有一个 MasterPage,其中指定了aspnetForm",因此我们不能有另一个具有 runat='server' 属性的表单.

We have a MasterPage with the 'aspnetForm' specified in it, because of which we cant have another form with runat='server' property.

我们已经尝试使用 JavaScript/code-behind 修改 aspnetFrom 的 action 属性,但无论我们做什么,它都保持不变.

We have tried modifying the aspnetFrom's action attribute using JavaScript/code-behind but it stays the same, whatever we do.

我们还尝试放置另一个嵌套表单,在收到来自 YouTube 的第一个带有令牌的响应后,其动作标签将设置在按钮单击事件上.但即使这样也行不通,因为在调用此方法时还没有创建表单..

We have also tried to put another nested form, whose action tag would be set on the button click event, after the first response from YouTube with token is received. But even this doesn't work, as the form isn't created at the time this method is called..

我们尝试了几种方法,但似乎都没有奏效.任何关于此的建议都会很棒.

We have tried several approaches but none seems to work.. any suggestions on this would be great.

如果您还有任何问题想清楚了解情况..请告诉我..

If you have any more questions to understand the situation clearly.. please let me know..

更新:

感谢大家的回复.

我们稍微更改了业务逻辑以解决问题.很久以前,我现在还没有寻找解决方案..

We changed the business logic a little bit to resolve the issue. It was long back and I am not looking for a resolution as of now..

但是,问题仍然存在并且正在收到回复.我非常感谢所有的帮助,因此,我会留下这个问题以供进一步讨论.

However, the question is still open and getting responses. I really appreciate all the help and hence, would leave the question open for any further discussions.

推荐答案

我将创建一个表单,您可以向用户显示该表单,其中包含用于元数据的文本字段和用于视频的文件上传控件.

I would create a form that you display to the user with text fields for the metadata and a file upload control for the video.

然后在处理回发的代码中,您可以将元数据提交到 YouTube API 以获取令牌值.

Then in the code that handles the postback, you can submit the metadata off to the YouTube API to get the Token Value.

然后您制作一个 HttpWebRequest令牌值传递到 WebRequest 对象的 Create 方法,并将 Method 属性设置为 Post.然后添加上传的视频(参见 dr evil 对上传带有 HTTPWebrequest (multipart/form-data)" 的文件以了解更多信息),当您调用 GetResponse() 时,您的文件将被提交.

You then craft an HttpWebRequest with the Token Value passed into the Create method of the WebRequest object and set the Method property to Post. You then add the uploaded video (see dr evil's answer to "Upload files with HTTPWebrequest (multipart/form-data)" for more a walk through of that), and when you call GetResponse() your file will be submitted.

注意事项:

  1. ScriptTimeout:如果您允许您的用户将大文件上传到 YouTube - 如果您需要一段时间才能传输它,那么您的脚本可能会提前终止.
  2. 带宽:您需要先上传到您的服务器,然后再上传到 YouTube - 通过允许用户在两​​步过程中直接上传到 YouTube,您永远不会接触视频,它永远不会接近您的服务器 - 这可能是您或您的托管服务提供商的问题 - 例如,我的主机不会将通过 FTP 的流量计入我的每月带宽,但会包含通过网站上传/下载的文件.
  1. ScriptTimeout: If you're allowing your users to upload large files to YouTube - if it takes a while for you to transmit it, then your script could be terminated prematurely.
  2. Bandwidth: You'll have both the upload to your servers and then the upload to YouTube to account for - by allowing the user to upload directly to YouTube in a two step process you never touch the video, and it never goes near your servers - this might be an issue for you or your hosting provider - for example, my host doesn't count traffic through FTP in my monthly bandwidth, but does include files uploaded/downloaded through the website.

这篇关于YouTube API 集成 ASP.NET - 单页上传视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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