1

I am making a website using YouTube API and i need to get users channel id to retrieve there data How can i do this in JavaScript Thanks in advance

Sanju GB
  • 27
  • 6
  • 2
    Looks like you will need to look at Youtube API and send XML requests to them. This seems very similar to this question I found after a google search of 'javascript channel id from channel url youtube' url: https://stackoverflow.com/questions/14366648/how-can-i-get-a-channel-id-from-youtube – Gloat Nov 30 '19 at 09:10
  • @DevinB good answer. I like that you answer his question with equal detail as he asked it. – Rocky Sims Nov 30 '19 at 09:15
  • https://stackoverflow.com/questions/9870512/how-to-obtain-the-query-string-from-the-current-url-with-javascript Here the above you can get your answer. – Hitech Hitesh Nov 30 '19 at 09:23

1 Answers1

0

this code is working already in my project.

can you try this code,

Maybe solve your problem with this code.

youtubeChannelId = channelName => {
  if (channelName) {
    var name = getChannelFromUrl(channelName);
    var url =
      'https://gdata.youtube.com/feeds/api/users/' +
      name +
      '?fields=id&alt=json';
    var result = UrlFetchApp.fetch(url);
    var data = Utilities.jsonParse(result.getContentText());

    if (
      typeof data['entry'] !== 'undefined' &&
      data['entry']['id']['$t'] !== 'undefined'
    ) {
      var id = 'UC' + data['entry']['id']['$t'].split('/').pop();

      return id;
    }
  }

  return '';
};

getChannelFromUrl = url => {
  var pattern = new RegExp(
    '^(https?://)?(www.)?youtube.com/(user/)?([a-z-_0-9]+)/?([?#]?.*)',
    'i',
  );
  var matches = url.match(pattern);
  if (matches) {
    return matches[4];
  }

  return url;
};
Chaurasia
  • 494
  • 1
  • 6
  • 22