示例#1
0
        public override UploadResult Upload(Stream stream, string fileName)
        {
            if (!CheckAuthorization())
            {
                return(null);
            }

            if (!Helpers.IsVideoFile(fileName))
            {
                Errors.Add("YouTube only supports video files.");
                return(null);
            }

            string metadata = GetMetadata(fileName);

            UploadResult result = SendRequestFile("https://www.googleapis.com/upload/youtube/v3/videos?part=id,snippet,status", stream, fileName,
                                                  headers: googleAuth.GetAuthHeaders(), metadata: metadata);

            if (!string.IsNullOrEmpty(result.Response))
            {
                YouTubeVideo video = JsonConvert.DeserializeObject <YouTubeVideo>(result.Response);

                if (video != null)
                {
                    if (UseShortenedLink)
                    {
                        result.URL = $"https://youtu.be/{video.id}";
                    }
                    else
                    {
                        result.URL = $"https://www.youtube.com/watch?v={video.id}";
                    }

                    switch (video.status.uploadStatus)
                    {
                    case YouTubeVideoStatus.UploadFailed:
                        Errors.Add("Upload failed: " + video.status.failureReason);
                        break;

                    case YouTubeVideoStatus.UploadRejected:
                        Errors.Add("Upload rejected: " + video.status.rejectionReason);
                        break;
                    }
                }
            }

            return(result);
        }
示例#2
0
文件: YouTube.cs 项目: snadge1/ShareX
        public override UploadResult Upload(Stream stream, string fileName)
        {
            if (!CheckAuthorization())
            {
                return(null);
            }

            if (!Helpers.IsVideoFile(fileName))
            {
                Errors.Add("YouTube only supports video files");
                return(null);
            }

            string metadata = GetMetadata(fileName);

            UploadResult result = SendRequestFile("https://www.googleapis.com/upload/youtube/v3/videos?part=id,snippet,status", stream, fileName,
                                                  headers: GoogleAuth.GetAuthHeaders(), metadata: metadata);

            if (!string.IsNullOrEmpty(result.Response))
            {
                YouTubeVideo upload = JsonConvert.DeserializeObject <YouTubeVideo>(result.Response);

                if (upload != null)
                {
                    AllowReportProgress = false;

                    result.URL = "https://youtu.be/" + upload.id;

                    switch (upload.status.uploadStatus)
                    {
                    case YouTubeVideoStatus.UploadFailed:
                        Errors.Add("Upload failed: " + upload.status.failureReason);
                        break;

                    case YouTubeVideoStatus.UploadRejected:
                        Errors.Add("Upload rejected: " + upload.status.rejectionReason);
                        break;
                    }
                }
            }

            return(result);
        }