认证后如何获取YouTube频道名称和URL



一旦我对用户进行身份验证(例如下面的代码中),我该如何找出他们的频道名称和频道的URL?

我正在使用.NET库的YouTube数据API V3:

UserCredential credential;
  using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
  {
    credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.Load(stream).Secrets,
        new[] { YouTubeService.Scope.YoutubeReadonly },
        "user",
        CancellationToken.None,
        new FileDataStore(this.GetType().ToString())
    );
  }
  var youtubeService = new YouTubeService(new BaseClientService.Initializer()
  {
    HttpClientInitializer = credential,
    ApplicationName = this.GetType().ToString()
  });

最终弄清楚了如何做。一旦您从身份验证中恢复令牌,请执行此操作:

var channelsListRequest = _youTubeService.Channels.List("id,snippet");
channelsListRequest.Mine = true;
var channelsListResponse = channelsListRequest.Execute();
if (    (null != channelsListResponse)          && 
        (null != channelsListResponse.Items)    &&
        (channelsListResponse.Items.Count > 0)  )
{
    Channel userChannel = channelsListResponse.Items[0];
    string youtubeUserID = userChannel.Id;
    string ytChannelURL = "https://www.youtube.com/channel/" + userChannel.Id;
    string name = userChannel.Snippet.Title;
}

phew!

相关内容

  • 没有找到相关文章