首页 > 解决方案 > 将视频上传到 YouTube - 超出配额错误

问题描述

我正在尝试开发一个将视频上传到 YouTube 的应用程序。

每当我尝试调用上传例程时,都会收到一条错误消息,指出我已超出配额。但在配额使用屏幕中,它说数据不可用。

我什至创建了一个新项目并使用新凭据来确保旧项目(在 google 开发人员 acc 中)并不有趣,但仍然保持不变。

    public class ClipUploader
{
    public static async Task UploadClip(string title, string path)
    {
        string vId = "";
        UserCredential credential;
        using (var stream = new FileStream(@"C:\Users\andre\source\repos\YouTubeClippingBot\TheBot\uploadClientSecret.json", FileMode.Open, FileAccess.Read))
        {
            credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.FromStream(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 },
                "user",
                CancellationToken.None
            );
        }


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

        var video = new Video();
        video.Snippet = new VideoSnippet();
        video.Snippet.Title = title;
        video.Snippet.Description = "Default Video Description";
        video.Snippet.Tags = new string[] { "tag1", "tag2" };
        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"
        var filePath = path; // Replace with path to actual movie file.

        using (var fileStream = new FileStream(filePath, FileMode.Open))
        {
            var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");
            videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
            videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;

            await videosInsertRequest.UploadAsync();

        }
    }

    static void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
    {
        switch (progress.Status)
        {
            case UploadStatus.Uploading:
                Console.WriteLine("{0} bytes sent.", progress.BytesSent);
                break;

            case UploadStatus.Failed:
                Console.WriteLine("An error prevented the upload from completing.\n{0}", progress.Exception);
                break;
        }
    }

    static void videosInsertRequest_ResponseReceived(Video video)
    {
        Console.WriteLine("Video id '{0}' was successfully uploaded.", video.Id);
        //Then I need to somehow send this back to chat....
    }
}

所以我不确定为什么会收到配额错误。该代码来自 YouTube github 页面,想要让它基本工作,然后我可以根据我实际需要做的事情对其进行定制。

任何建议都会很高兴收到。

标签: c#google-apiyoutube-apiyoutube-data-apigoogle-api-dotnet-client

解决方案


您收到错误是因为您已超出当前配额。

您需要了解此 api Intro to YouTube API 的配额系统是如何工作的,以及 2021 年初学者的基于成本的配额。。您可以在请求上花费的配额点数是有限的。当您运行该请求时,每种请求类型都有不同的配额成本,积分将从您的总配额中扣除。此限制在 Google 云控制台中定义。它基于成本,而不是您提出的请求数量。

Video.insert 例如花费 1600。因此,如果您像我一样有 10000 个限制,那么您每天最多可以上传 6 个视频。

你目前的配额是多少。

转到左侧的Google Cloud Console转到库,然后搜索 YouTube 数据 api。

单击管理按钮,然后转到配额。检查您当前的配额是多少

在此处输入图像描述

请求增加配额。

在此页面顶部,您还可以申请额外配额

在此处输入图像描述

使用情况报告

不要指望使用情况报告,它们不是很准确,因为您收到的错误消息。


推荐阅读