4

我正在创建一个将视频上传到 YouTube 频道的 ASP.NET C# 应用程序。*我已经(尽我所能)通读了位于

YouTube API 文档

我已经能够使用提供的示例代码成功实现两个将视频上传到 YouTube 频道的示例。

例如,使用直接方法(仅附上重要代码):

<!-- eja: import the google libraries -->
 <%@ Import Namespace="Google.GData.Client" %>
 <%@ Import Namespace="Google.GData.Extensions" %>
<%@ Import Namespace="Google.GData.YouTube" %>
<%@ Import Namespace="System.Net" %>

<!-- some more code -->
 <%
    // specify where to go to once authenticated
    Uri targetUri = new Uri(Request.Url, "VideoManager.aspx");

    // hide the link to authenticate for now.
    GotoAuthSubLink.Visible = false;
    // GotoAuthSubLink.Visible = true;

    // look for a session var storing the auth token. if it's not empty
    if (Session["token"] != null)
    {
        // go to the VideoManager link
        Response.Redirect(targetUri.ToString());
    }
    else if (Request.QueryString["token"] != null)
    {
        // if we have the auth key in the URL, grab it from there instead
        String token = Request.QueryString["token"];
        // set the session equal to AuthSubUtil's calling the exchangeForSessionToken method
        // returns the token and convert it to a string 
        Session["token"] = AuthSubUtil.exchangeForSessionToken(token, null).ToString();
        Response.Redirect(targetUri.ToString(), true);
    }
    else 
    {
        //no auth token, display the link and create the token by loading the google
        // auth page

        String authLink = AuthSubUtil.getRequestUrl(Request.Url.ToString(), "http://gdata.youtube.com", false, true);
        GotoAuthSubLink.Text = "Login to your Google Account";
        GotoAuthSubLink.Visible = true;
        GotoAuthSubLink.NavigateUrl = AuthSubUtil.getRequestUrl(Request.Url.ToString(),"http://gdata.youtube.com",false,true);

    }

 <asp:HyperLink ID="GotoAuthSubLink" runat="server"/>

那是第一页……它加载了谷歌身份验证屏幕。(见附件图片的链接,很安全,我刚刚在stackOverflow上设置了一个新帐户,还不能上传图片)。

谷歌身份验证屏幕的 Twitpic 图像

然后它会导致一个带有上传机制的页面......上传工作我并不担心,但这里是代码片段仅供参考。

       // create an instance ot the YouTubeService class. passing the application name and my DEV KEY
        YouTubeService service = new YouTubeService(authFactory.ApplicationName, **API_KEY**);

        // retrieve the current session token as a string if any
        authFactory.Token = HttpContext.Current.Session["token"] as string;
        // incorporate the information into our service
        service.RequestFactory = authFactory;

        try
        {
            // a YouTubeEntry object is single entity within a videoFeed object. It generally contains info
            // about the video. when uploading, we will assign the values that we received to the feed.

            YouTubeEntry entry = new YouTubeEntry();

            // aggregate all the initial descriptor information
            entry.Media = new Google.GData.YouTube.MediaGroup();
            entry.Media.Description = new MediaDescription(this.Description.Text);
            entry.Media.Title = new MediaTitle(this.Title.Text);
            entry.Media.Keywords = new MediaKeywords(this.Keyword.Text);

            // process  entry.Media.Categories to assign the category
            MediaCategory category = new MediaCategory(this.Category.SelectedValue);
            category.Attributes["scheme"] = YouTubeService.DefaultCategory;
            entry.Media.Categories.Add(category);

            // prepare the token used for uploading
            FormUploadToken token = service.FormUpload(entry);
            HttpContext.Current.Session["form_upload_url"] = token.Url;
            HttpContext.Current.Session["form_upload_token"] = token.Token;

            // construct the URL
            string page = "http://" + Request.ServerVariables["SERVER_NAME"];

            if (Request.ServerVariables["SERVER_PORT"] != "80")
            {
                page += ":" + Request.ServerVariables["SERVER_PORT"];
            }
            page += Request.ServerVariables["URL"];

            HttpContext.Current.Session["form_upload_redirect"] = page;
            Response.Redirect("UploadVideo.aspx");

UploadVideo.aspx 页面是实际的上传表单,它可以正常工作,所以我并不担心。

替代方法不是推荐的方法,因为它本质上是同步的,但它确实避免了登录屏幕,因为它允许我们传递凭据以进行身份​​验证(它作为 Web 应用程序工作)......再次附在下面的主要代码。

<%
    GAuthSubRequestFactory authFactory = new GAuthSubRequestFactory(ServiceNames.YouTube, "TesterApp");
     // Response.Write("serviceNames.youtube=" + ServiceNames.YouTube + "<br />");
    YouTubeRequestSettings s = new YouTubeRequestSettings(authFactory.ApplicationName, **your API KEY**,**Your email account as a username**,**your password**);

YouTubeRequest request = new YouTubeRequest(s);
Video newVideo = new Video();
newVideo.Title = "test at 4:40";
newVideo.Tags.Add(new MediaCategory("Games", YouTubeNameTable.CategorySchema));
newVideo.Keywords = "cars, funny";
newVideo.Description = "My description";
newVideo.YouTubeEntry.Private = false;
// newVideo.Tags.Add(new MediaCategory("mydevtag, anotherdevtag", YouTubeNameTable.DeveloperTagSchema));

// newVideo.YouTubeEntry.Location = new GeoRssWhere(37, -122);
// alternatively, you could just specify a descriptive string
newVideo.YouTubeEntry.setYouTubeExtension("location", "Somewhere,Someplace");

newVideo.YouTubeEntry.MediaSource = new MediaFileSource("C:\\IMG_1672.MOV",
  "video/quicktime");
Video createdVideo = request.Upload(newVideo);
Response.Write("This will print out once the file is uploaded...indicates that the code is <i>synchronous</i>. The cursor spins around until done. go get a coffee then check the YouTube Channel");

 %>

所以基本上我要问的问题是 - 有没有一种方法可以将视频上传到 ASP.NET C# 代码中的 YouTube 频道 a) 用于 Web 应用程序 b) 我可以通过代码将凭据传递给 c) 绕过上面看到的 Google 身份验证屏幕和 d) 不使用 OAuth 和 openID 以及证书等?

该应用程序仅适用于短期活动(仅限 11 月),我很高兴使用已弃用的 authSubUtil 和 dev 密钥,无需担心 oAuth 2.x 或开放 ID(因为 authsubutil 无论如何都会在 2015 年弃用)。

任何帮助表示赞赏。

谢谢

爱德华

4

1 回答 1

0

您最好使用 ClientLogin 身份验证,您可以在其中存储用户帐户的用户名和密码,然后使用 DirectUpload。

直接上传:https ://developers.google.com/youtube/2.0/developers_guide_dotnet#Direct_Upload

客户端登录:https ://developers.google.com/youtube/2.0/developers_guide_protocol_clientlogin#ClientLogin_Authentication

注意客户端登录已被弃用,他们希望您使用 OAuth,但是如果您快速执行此操作,您应该没问题!

于 2012-09-06T13:27:10.167 回答