IanS
IanS

Reputation: 315

How to get YouTube channel name and URL after authenticating

Once I have authenticated a user - such as in the code below - how can I find out their channel name and URL of channel?

I'm using the YouTube data api v3 with .NET library:

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()
  });

Upvotes: 2

Views: 902

Answers (1)

IanS
IanS

Reputation: 315

Finally worked out how to do it. Once you get the token back from the authentication do this:

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!

Upvotes: 3

Related Questions