C# YouTube API 集成 ASP.NET - 使用单个页面上传视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/629435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:59:04  来源:igfitidea点击:

YouTube API integration ASP.NET - upload video with a single page

c#asp.netyoutube-api

提问by Danish Khan

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

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

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.

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

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.

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

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

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

我们有一个 MasterPage,其中指定了“aspnetForm”,因此我们不能有另一个带有runat='server'属性的表单。

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

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

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..

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

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..

如果您有任何其他问题以清楚了解情况..请告诉我..



Update:

更新:

Thanks for the responses guys.

谢谢你们的回应。

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.

然而,这个问题仍然悬而未决,正在得到回应。我真的很感谢所有的帮助,因此,我会把这个问题留给任何进一步的讨论。

采纳答案by Zhaph - Ben Duguid

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.

我会创建一个表单,您可以向用户显示元数据的文本字段和视频的文件上传控件。

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

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

You then craft an HttpWebRequestwith the Token Value passed into the Createmethod of the WebRequestobject and set the Methodproperty 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.

然后使用传递给对象方法的令牌值制作HttpWebRequest并将属性设置为。然后您添加上传的视频(有关详细介绍,请参阅evil 博士对“使用 HTTPWebrequest (multipart/form-data) 上传文件”的回答),当您调用时,您的文件将被提交。CreateWebRequestMethodPostGetResponse()

Things to watch out for:

需要注意的事项:

  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.
  1. ScriptTimeout:如果您允许您的用户将大文件上传到 YouTube - 如果您需要一段时间来传输它,那么您的脚本可能会过早终止。
  2. 带宽:您将同时上传到您的服务器,然后上传到 YouTube 来考虑 - 通过允许用户在两​​步过程中直接上传到 YouTube,您永远不会触摸视频,并且它永远不会靠近您的服务器 -这对您或您的托管服务提供商来说可能是一个问题 - 例如,我的主机在我的每月带宽中不计算通过 FTP 的流量,但包含通过网站上传/下载的文件。

回答by sgriffinusa

You could post to the YouTube form programatically. I recently modified the example found at http://www.codeproject.com/KB/cs/uploadfileex.aspxto write some code that posted a file to a third party website as a nightly job.

您可以以编程方式发布到 YouTube 表单。我最近修改了http://www.codeproject.com/KB/cs/uploadfileex.aspx上的示例,编写了一些代码,将文件作为夜间工作发布到第三方网站。

This would involve a performance hit as the file would have to be uploaded to your server and then to YouTube instead of directly to YouTube, but it is an option.

这会影响性能,因为文件必须上传到您的服务器,然后上传到 YouTube,而不是直接上传到 YouTube,但这是一种选择。