javascriptyoutubeyoutube-data-api

How to get a youtube channelid from the channels link programmatically


I need to get the channelid of a youtube channel (to use the youtube data api) with the link of the channel. The problem is that there are a few types of links for a youtube channel. The first one is where the channel id is in the link:

https://www.youtube.com/channel/UC-lHJZR3Gqxm24_Vd_AJ5Yw

This is ok because i can just extract the channelid from the link. The problem comes with the second one:

https://www.youtube.com/@PewDiePie

This is the link you normally get for a youtube channel and it has a @ instead of the channelid.

When i use the youtube api to get the channel id of a not so popular channel i get the wrong results:

https://www.googleapis.com/youtube/v3/search?part=snippet&q=@pewdiepie6&type=channel&key=[API_KEY]

I would like to get the channel info and channelid for the channel with the @pewdiepie6 handle.

But i get the more popular channel with the @pewdiepie6802 handle. Also, i get more than 1 channel as a result.

{
  "kind": "youtube#searchListResponse",
  "etag": "ecshCA97Jd0CuntIj9NzN3UF37U",
  "nextPageToken": "CAUQAA",
  "regionCode": "CH",
  "pageInfo": {
    "totalResults": 51,
    "resultsPerPage": 5
  },
  "items": [
    {
      "kind": "youtube#searchResult",
      "etag": "x4yStPuXhfUUIaev-BCMiq1JQ4E",
      "id": {
        "kind": "youtube#channel",
        "channelId": "UCnJABWFF_vWuPgPjGjVJF8A"
      },
      "snippet": {
        "publishedAt": "2018-03-02T14:07:51Z",
        "channelId": "UCnJABWFF_vWuPgPjGjVJF8A",
        "title": "PewDiePie",
        "description": "",
        "thumbnails": {
          "default": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu84CCwyUjzgQkuvN7ms2JUCU0EgofxrTdhZLZKs=s88-c-k-c0xffffffff-no-rj-mo"
          },
          "medium": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu84CCwyUjzgQkuvN7ms2JUCU0EgofxrTdhZLZKs=s240-c-k-c0xffffffff-no-rj-mo"
          },
          "high": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu84CCwyUjzgQkuvN7ms2JUCU0EgofxrTdhZLZKs=s800-c-k-c0xffffffff-no-rj-mo"
          }
        },
        "channelTitle": "PewDiePie",
        "liveBroadcastContent": "none",
        "publishTime": "2018-03-02T14:07:51Z"
      }
    },
    {
      "kind": "youtube#searchResult",
      "etag": "2et4mkoeeilV2DhJRH7M5xlgnuQ",
      "id": {
        "kind": "youtube#channel",
        "channelId": "UCSn7KcaNdOrVf4aH-6OdFMg"
      },
      "snippet": {
        "publishedAt": "2017-04-23T06:06:06Z",
        "channelId": "UCSn7KcaNdOrVf4aH-6OdFMg",
        "title": "PewDiePie 6",
        "description": "",
        "thumbnails": {
          "default": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu8OB0Z-JNTYnB2Q0KLoV6LjvpUYpYfN0sfAMgfT=s88-c-k-c0xffffffff-no-rj-mo"
          },
          "medium": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu8OB0Z-JNTYnB2Q0KLoV6LjvpUYpYfN0sfAMgfT=s240-c-k-c0xffffffff-no-rj-mo"
          },
          "high": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu8OB0Z-JNTYnB2Q0KLoV6LjvpUYpYfN0sfAMgfT=s800-c-k-c0xffffffff-no-rj-mo"
          }
        },
        "channelTitle": "PewDiePie 6",
        "liveBroadcastContent": "none",
        "publishTime": "2017-04-23T06:06:06Z"
      }
    },
    {
      "kind": "youtube#searchResult",
      "etag": "88l8OAYbY0S8XY9NbWcqW2QtJJ4",
      "id": {
        "kind": "youtube#channel",
        "channelId": "UCpqNuOLjibXIO43tJbZ-s3w"
      },
      "snippet": {
        "publishedAt": "2017-06-17T21:56:28Z",
        "channelId": "UCpqNuOLjibXIO43tJbZ-s3w",
        "title": "PewDiePie",
        "description": "",
        "thumbnails": {
          "default": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu-GIq-DWy8zPGzPmQnb5LHvR_MlnlK3t-G5-45i=s88-c-k-c0xffffffff-no-rj-mo"
          },
          "medium": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu-GIq-DWy8zPGzPmQnb5LHvR_MlnlK3t-G5-45i=s240-c-k-c0xffffffff-no-rj-mo"
          },
          "high": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu-GIq-DWy8zPGzPmQnb5LHvR_MlnlK3t-G5-45i=s800-c-k-c0xffffffff-no-rj-mo"
          }
        },
        "channelTitle": "PewDiePie",
        "liveBroadcastContent": "none",
        "publishTime": "2017-06-17T21:56:28Z"
      }
    },
    {
      "kind": "youtube#searchResult",
      "etag": "MItPbLglqDfEkQ-fXSbQef7js2c",
      "id": {
        "kind": "youtube#channel",
        "channelId": "UCQXfsdNRgLL0Cz67stMFNVQ"
      },
      "snippet": {
        "publishedAt": "2018-03-31T09:27:20Z",
        "channelId": "UCQXfsdNRgLL0Cz67stMFNVQ",
        "title": "Marian Pewdiepie 6",
        "description": "Nu voi mai posta mai mult de 1 an căci nu prea am vhef Dar totuși dați subscribe.",
        "thumbnails": {
          "default": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu9G8MRCxqrk5dCx9T6lxjWpaAL62QuD54LGpgV6=s88-c-k-c0xffffffff-no-rj-mo"
          },
          "medium": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu9G8MRCxqrk5dCx9T6lxjWpaAL62QuD54LGpgV6=s240-c-k-c0xffffffff-no-rj-mo"
          },
          "high": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu9G8MRCxqrk5dCx9T6lxjWpaAL62QuD54LGpgV6=s800-c-k-c0xffffffff-no-rj-mo"
          }
        },
        "channelTitle": "Marian Pewdiepie 6",
        "liveBroadcastContent": "upcoming",
        "publishTime": "2018-03-31T09:27:20Z"
      }
    },
    {
      "kind": "youtube#searchResult",
      "etag": "_yNYVPOhK2ZEn0yq580TFty6AHU",
      "id": {
        "kind": "youtube#channel",
        "channelId": "UCR8SFIzFOxf50N0lYER8_-w"
      },
      "snippet": {
        "publishedAt": "2019-03-09T20:56:38Z",
        "channelId": "UCR8SFIzFOxf50N0lYER8_-w",
        "title": "Go sub to Pewdiepie 6",
        "description": "",
        "thumbnails": {
          "default": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu_3fwpv9uhEyzb_bUgNuzETQHpdgvKv-xDSYQ=s88-c-k-c0xffffffff-no-rj-mo"
          },
          "medium": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu_3fwpv9uhEyzb_bUgNuzETQHpdgvKv-xDSYQ=s240-c-k-c0xffffffff-no-rj-mo"
          },
          "high": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu_3fwpv9uhEyzb_bUgNuzETQHpdgvKv-xDSYQ=s800-c-k-c0xffffffff-no-rj-mo"
          }
        },
        "channelTitle": "Go sub to Pewdiepie 6",
        "liveBroadcastContent": "none",
        "publishTime": "2019-03-09T20:56:38Z"
      }
    }
  ]
}

I also tried to extract the channelid out of the javascript of the page but using Beautiful Soup. But i could not get it to return the right javascript.

There is also a second api link with the forUsername parameter. But with this link it tries to look for a user which uploads on a channel which is not what i want.

At the end of the day i want a safe method to get the channel id with the channels link. https://www.youtube.com/@pewdiepie6 => UCl-0ct6u2AAfsqGGgVh5dEw

(I got this manually out of the @pewdiepie6 javascript file from the page at externalid)


Solution

  • Youtube Data api still doesn't support Handle. Therefore, the safest method for now is curl or file_get_contents. Example php code of file_get_contents to get channel id:

    <?php
      $connect= file_get_contents("https://www.youtube.com/@pewdiepie6");
      $channeli = explode('<meta property="og:site_name" content="YouTube"><meta property="og:url" content="', $connect);   
      $channeli = explode('">', $channeli[1]); 
      $channelidurl="$channeli[0]";
      echo " Channel ID url: $channelidurl";
    ?>
    

    The example is on this page, it pulls the channel ID url with the same method using the same logic as I mentioned above: Youtube Monetization Checker https://ytlarge.com/youtube/monetization-checker/ enter image description here There is no other method for now.