Youtube API v3 return 401 when i try to upload video

元气小坏坏 提交于 2019-12-24 00:19:41

问题


I use ServiceAccount uploader for youtube. I use Youtube API v3. I have simple console application, that upload video to 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();

But this code return error: Response status code does not indicate success: 401 (Unauthorized). Help me, please

This is result by 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"
 }
}

回答1:


Service Accounts don't work with YouTube API. Here's how to implement OAuth2 instead.




回答2:


Service account doesnt work with youtube. Also please ensure that if you are using a gmail ID it must be linked to youtube otherwise you would get 401.




回答3:


Try to create your channel by Youtube App.



来源:https://stackoverflow.com/questions/19883882/youtube-api-v3-return-401-when-i-try-to-upload-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!