无法将视频上传到Youtube

时间:2017-03-14 11:02:44

标签: c# youtube youtube-data-api

我正在使用youtube API第三版。

尝试通过网络应用程序上传视频。

当我使用自己的帐户时,我可以上传视频 但是当我使用来自客户端的凭据时,它现在失败并且之前有效。

收到错误消息{"Value cannot be null.\r\nParameter name: baseUri"}.

上传视频有任何限制吗?

在此错误发生之前,我可以成功上传70多个视频 能帮我解决这个问题吗

var guid = "";
try
        {
            UserCredential credential;
            var filepath = HostingEnvironment.MapPath("~/");
            var datafolder = HostingEnvironment.MapPath("/App_Data/MyGoogleStorage");
            using (var stream = new FileStream(filepath + "/App_Data/client_id.json", FileMode.Open, FileAccess.Read))
            {
                credential = await Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    // This OAuth 2.0 access scope allows an application to upload files to the
                    // authenticated user's YouTube channel, but doesn't allow other types of access.
                    new[] { YouTubeService.Scope.YoutubeUpload, YouTubeService.Scope.Youtubepartner },
                    "user",
                    CancellationToken.None,
                    new FileDataStore(datafolder)
                );
            }


            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
            });

            var fileName = e.Name;

            guid = Path.GetFileNameWithoutExtension(fileName);

            var activityGetInfoUrl = ConfigurationManager.AppSettings["activityGetInfoUrl"];
            var activityUpdateInfoUrl = ConfigurationManager.AppSettings["activityUpdateInfoUrl"];

            activityGetInfoUrl = activityGetInfoUrl + "?guid=" + guid;
            var activityJsonResponse = new WebClient().DownloadString(activityGetInfoUrl);
            JObject activityJson = JObject.Parse(activityJsonResponse);

            var video = new Video();
            video.Snippet = new VideoSnippet();
            video.Snippet.Title = activityJson.GetValue("Title").ToString();
            video.Snippet.Description = activityJson.GetValue("Description").ToString();
            video.Snippet.Tags = new string[] { "#####", "######" };
            video.Snippet.CategoryId = "22"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
            video.Status = new VideoStatus();
            video.Status.PrivacyStatus = "unlisted"; // or "private" or "public"

            using (var fileStream = new FileStream(e.FullPath, FileMode.Open))
            {
                var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");
                var videoStatus = videosInsertRequest.Upload();

                activityUpdateInfoUrl = activityUpdateInfoUrl + "?guid=" + guid + "&youtubeId=" + videosInsertRequest.ResponseBody.Id;
                new WebClient().DownloadString(activityUpdateInfoUrl);

                var newPlaylistItem = new PlaylistItem();
                newPlaylistItem.Snippet = new PlaylistItemSnippet();
                newPlaylistItem.Snippet.PlaylistId = ConfigurationManager.AppSettings["playlistId"];
                newPlaylistItem.Snippet.ResourceId = new ResourceId();
                newPlaylistItem.Snippet.ResourceId.Kind = "youtube#video";
                newPlaylistItem.Snippet.ResourceId.VideoId = videosInsertRequest.ResponseBody.Id;
                newPlaylistItem = await youtubeService.PlaylistItems.Insert(newPlaylistItem, "snippet").ExecuteAsync();
            }
        }
        catch(Exception ex)
        {
            log.Debug(ex.ToString());                
        }

0 个答案:

没有答案