youtube-api - 当我尝试上传视频时,Youtube API v3 返回 401

标签 youtube-api youtube-data-api

我为 youtube 使用 ServiceAccount uploader 。我使用 Youtube API v3。 我有一个简单的控制台应用程序,可以将视频上传到 youtube

String serviceAccountEmail = "...";

        var certificate = new X509Certificate2(@"G:\Youtube\1ffd6ea22be7c41e0852935a0f35438e4beb2e2d-privatekey.p12", "notasecret", X509KeyStorageFlags.Exportable);
        //, YouTubeService.Scope.YoutubeUpload
        ServiceAccountCredential credential = new ServiceAccountCredential(
           new ServiceAccountCredential.Initializer(serviceAccountEmail)
           {
                                  Scopes = new[] { YouTubeService.Scope.Youtube, YouTubeService.Scope.YoutubeUpload }
           }.FromCertificate(certificate));



        var youtube = new YouTubeService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
        });

        var video = new Video
        {
            Snippet = new VideoSnippet
            {
                Title = "Video title",
                Description = "Video description",
                Tags = new string[] {"tag1", "tag2"},
                CategoryId = "22"
            },
            Status = new VideoStatus {PrivacyStatus = "public"}
        };

        var fileStream = new FileStream("G:\\1.mp4", FileMode.Open);

        var videosInsertRequest = youtube.Videos.Insert(video, "snippet,status", fileStream, "video/*");
        videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
        videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;

        var uploadThread = new Thread(() => videosInsertRequest.Upload());
        uploadThread.Start();
        uploadThread.Join();

        Console.Read();

但是这段代码返回错误: 响应状态码不表示成功:401(未授权) 请帮助我

这是 Fiddler 的结果:

    HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="https://www.google.com/accounts/AuthSubRequest", error=invalid_token
Content-Type: application/json
Content-Length: 255
Date: Sun, 10 Nov 2013 14:36:29 GMT
Server: HTTP Upload Server Built on Nov 5 2013 16:15:49 (1383696949)
Alternate-Protocol: 443:quic

{
 "error": {
  "errors": [
   {
    "domain": "youtube.header",
    "reason": "youtubeSignupRequired",
    "message": "Unauthorized",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Unauthorized"
 }
}

最佳答案

服务帐户不适用于 YouTube API。 Here's how to implement OAuth2相反。

关于youtube-api - 当我尝试上传视频时,Youtube API v3 返回 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19883882/

相关文章:

jquery - 如何通过jQuery暂停所有用户嵌入的YouTube视频?

c# - 批量上传到 YouTube - ASP.NET 4 C#

php - curl 错误 60 : SSL certificate prblm: unable to get local issuer certificate

youtube-api - 如何使用 YouTube Data API 添加片尾画面?

google-api - 在达到 API 配额限制之前 YouTube 视频上传被拒绝

youtube-api - Youtube API 是否有两个不同的速率限制?

javascript - 如何平滑幻灯片过渡到下一个/上一个 YouTube 播放列表视频? YouTube API(包括 fiddle )

java - YouTube 直播 : Record screen

python - Python:从YouTube下载字幕

youtube - 通过 YouTube 数据 API 访问公共(public)数据,无需身份验证。