1

我正在尝试将视频上传到Youtube。已成功将视频上传到帐户的频道。之后,我创建了名为“Deneme1”的新频道。我试图用api上传到那个频道;但上传到主。

我的代码:

    public static string UploadVideo(string FilePath, string Title, string Description)
    {
        YouTubeRequestSettings settings;
        YouTubeRequest request;
        string devkey = "api key";            
        string username = "mail@gmail.com";
        string password = "password";
        settings = new YouTubeRequestSettings("Deneme1", devkey, username, password) { Timeout = 10000000 };
        request = new YouTubeRequest(settings);

        Video newVideo = new Video();
        newVideo.Title = Title;
        newVideo.Description = Description;
        newVideo.Private = true;
        newVideo.YouTubeEntry.Private = false;
        newVideo.Keywords = "asd";
        newVideo.Tags.Add(new MediaCategory("Sports", YouTubeNameTable.CategorySchema));
        newVideo.YouTubeEntry.MediaSource = new MediaFileSource(FilePath, "video/flv");
        Video createdVideo = request.Upload(newVideo);
        return createdVideo.VideoId;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
         try
        {
            string videopath, videotitle, videodesc;
            videopath = @"C:\Users\Ercin\Dropbox\Cloudy\Visual Studio\Projects\videoupload\videoupload\badstart.flv";
            videotitle = "test title";
            videodesc = "test description";
            UploadVideo(videopath, videotitle, videodesc);
        }
        catch (Exception exception)
        {
            Response.Write("Upload failed: " + exception.Message);
        }

任何帮助都会很棒!

4

2 回答 2

2

下面的代码对我来说很好,可以上传到我在用户名中放入 ChannelId 的特定频道。

public static Google.Apis.YouTube.v3.YouTubeService AuthenticateOaut(string clientId, string clientSecret, string userName)
        {

            string[] scopes = new string[] { Google.Apis.YouTube.v3.YouTubeService.Scope.Youtube,  // view and manage your YouTube account
                                             Google.Apis.YouTube.v3.YouTubeService.Scope.YoutubeForceSsl,
                                             Google.Apis.YouTube.v3.YouTubeService.Scope.Youtubepartner,
                                             Google.Apis.YouTube.v3.YouTubeService.Scope.YoutubepartnerChannelAudit,
                                             Google.Apis.YouTube.v3.YouTubeService.Scope.YoutubeReadonly,
                                             Google.Apis.YouTube.v3.YouTubeService.Scope.YoutubeUpload};

            try
            {
                // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
                UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
                                                                                             , scopes
                                                                                             , userName
                                                                                             , CancellationToken.None
                                                                                             , new FileDataStore("Daimto.YouTube.Auth.Store")).Result;

                Google.Apis.YouTube.v3.YouTubeService service = new Google.Apis.YouTube.v3.YouTubeService(new Google.Apis.YouTube.v3.YouTubeService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "Web client 1",

                });
                return service;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException);
                return null;

            }

        }
于 2015-10-18T19:10:18.670 回答
1

从头开始(这对某些人会有帮助)我会告诉你我做了什么:

  • 您将获得授权凭据(我使用过 OAuth2.0):https ://developers.google.com/youtube/registering_an_application#Create_OAuth2_Tokens
  • 您使用 YouTube v3 API(有一个 nuget 包)。
  • 您编写的代码几乎就是thiago.adriano26他的答案中的内容。
  • 首次运行您的应用程序时,它会打开浏览器供您验证并选择您想要的频道(我不确定 Google 为什么这样做,因为您已经在代码中指定了用户 ID,但无论如何他们都是这样做的...)。
  • 完成此步骤后,将在 : 处生成令牌C:\Users\User\AppData\Roaming\Google.Apis.Auth\Google.Apis.A‌​uth.OAuth2.Responses‌​.TokenResponse-<UserId>。这似乎将使用UserId的频道与实际频道联系起来。只要有这个,两者之间的联系就会存在。删除它或使用另一个 UserId(每个频道都有自己的UserIdChannelId请参见此处:https ://support.google.com/youtube/answer/3250431?hl=en )将允许您从浏览器中选择一个新频道。
于 2017-03-19T20:37:27.573 回答