使用OAuth2使用Youtube API上传视频

时间:2018-04-05 14:47:46

标签: c# youtube-api

我正在尝试使用OAuth2上传视频。但我一直收到这样的错误 -

{Google.Apis.Requests.RequestErrorUnauthorized 
[401]Errors 
[   Message[Unauthorized] Location[Authorization - header] 
Reason[youtubeSignupRequired] Domain[youtube.header]]}

我的客户端ID,密钥,访问令牌,刷新令牌全部设置完毕。令牌仍然有效,刷新令牌未过期。

这是它的外观。

UserCredential credential;
var token = new TokenResponse { AccessToken = accesstoken, RefreshToken = refreshToken };
using (var stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/client_id.json"), FileMode.Open, FileAccess.Read))
{

    credential = new UserCredential(new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
    {
        ClientSecretsStream  = stream,
        //ClientSecrets = new ClientSecrets { ClientId = clientID, ClientSecret = clientSecret },
        Scopes = new string[] { YouTubeService.Scope.YoutubeUpload }
    }), "user", token);
}


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 = name;
video.Snippet.Description = description;
video.Snippet.Tags = new string[] { "test" };
video.Snippet.CategoryId = "22"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
video.Status = new VideoStatus();
video.Status.PrivacyStatus = "public"; // or "private" or "public"

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

    videosInsertRequest.Upload();
}

0 个答案:

没有答案