2013-11-09 87 views
2

我使用ServiceAccount上傳器的YouTube。我使用Youtube API v3。 我有簡單的控制檯應用程序,視頻上傳至YouTubeYoutube API v3返回401當我嘗試上傳視頻

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(未經授權)。 幫助我,請

這是結果來小提琴手:

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

服務帳戶不適用於YouTube。另外請確保,如果您使用的是Gmail身份證,則必須將其鏈接到YouTube,否則您將獲得401.

0

嘗試使用Youtube應用創建您的頻道。

相關問題