如何使用频道名称 URL 获取 YouTube 频道 ID?
我编写了一个程序,用户提供一个 YouTube 频道 URL,作为回报,我将向用户显示其频道 ID。
例如,如果有人输入如下 URL: https://youtube.com/@AndreoBee ,那么我将返回其频道 ID。
const axios = require('axios');
async function getChannelId(url) {
const channelUsername = url.split('/').pop();
const response = await axios.get(`https://www.googleapis.com/youtube/v3/channels?part=id&forUsername=${channelUsername}&key=myapi`);
if (response.data.items.length === 0) {
throw new Error(`Channel not found: ${url}`);
}
console.log(response.data.items[0].id);
}
getChannelId('https://www.youtube.com/@AndreoBee');
输出:
/home/runner/ytcheck/index.js:7 if (response.data.items.length === 0) { ^
TypeError: Cannot read properties of undefined (reading 'length') at getChannelId (/home/runner/ytcheck/index.js:7:27)
一种替代方案是使用 YouTube 数据 API:
- 使用 search:list 端点来搜索符合条件的频道 - 在本例中,频道。
示例:
Search channels named "AndreoBee" - you're getting this value in the
channelUsername
variable in your code:
URL:
https://youtube.googleapis.com/youtube/v3/search?part=id%2Csnippet&maxResults=10&q=AndreoBee&type=channel&key=[YOUR_API_KEY]
响应:
{
"kind": "youtube#searchListResponse",
"etag": "K2G-sXJIFnrJFyMaoDvw4XMBym4",
"nextPageToken": "CAoQAA",
"regionCode": "CO",
"pageInfo": {
"totalResults": 6740,
"resultsPerPage": 10
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "oeyNublAqxB8TOEQbtwA4k0Oqgs",
"id": {
"kind": "youtube#channel",
"channelId": "UCjdjyjdcdmldCK5XuuWbn1A"
},
"snippet": {
"publishedAt": "2020-01-21T15:12:27Z",
"channelId": "UCjdjyjdcdmldCK5XuuWbn1A",
"title": "AndreoBee",
"description": "I am a Youtuber.",
"thumbnails": {
"default": {
"url": "https://yt3.ggpht.com/ytc/AL5GRJW_gCXDGAviuBWh0GwUO4V5Ia4JNYzC0Mp9bYhigA=s88-c-k-c0xffffffff-no-rj-mo"
},
"medium": {
"url": "https://yt3.ggpht.com/ytc/AL5GRJW_gCXDGAviuBWh0GwUO4V5Ia4JNYzC0Mp9bYhigA=s240-c-k-c0xffffffff-no-rj-mo"
},
"high": {
"url": "https://yt3.ggpht.com/ytc/AL5GRJW_gCXDGAviuBWh0GwUO4V5Ia4JNYzC0Mp9bYhigA=s800-c-k-c0xffffffff-no-rj-mo"
}
},
"channelTitle": "AndreoBee",
"liveBroadcastContent": "none",
"publishTime": "2020-01-21T15:12:27Z"
}
},
{
"kind": "youtube#searchResult",
"etag": "iG8-NwZab0LLB245AY2MkJ6pexI",
"id": {
"kind": "youtube#channel",
"channelId": "UC2Xpzteb9hbamaeEHZRxPCw"
},
"snippet": {
"publishedAt": "2020-11-27T07:07:23Z",
"channelId": "UC2Xpzteb9hbamaeEHZRxPCw",
"title": "AndreoBee",
"description": "This is my backup channel guys ;)",
"thumbnails": {
"default": {
"url": "https://yt3.ggpht.com/ytc/AL5GRJVgMinAIKpEHBPjM2Os8fHFKcsxtTyTIflqTwVR=s88-c-k-c0xffffffff-no-rj-mo"
},
"medium": {
"url": "https://yt3.ggpht.com/ytc/AL5GRJVgMinAIKpEHBPjM2Os8fHFKcsxtTyTIflqTwVR=s240-c-k-c0xffffffff-no-rj-mo"
},
"high": {
"url": "https://yt3.ggpht.com/ytc/AL5GRJVgMinAIKpEHBPjM2Os8fHFKcsxtTyTIflqTwVR=s800-c-k-c0xffffffff-no-rj-mo"
}
},
"channelTitle": "AndreoBee",
"liveBroadcastContent": "none",
"publishTime": "2020-11-27T07:07:23Z"
}
},
{
"kind": "youtube#searchResult",
"etag": "3X2zApHnPAW2p2awm4tIETn9ybI",
"id": {
"kind": "youtube#channel",
"channelId": "UCp6iawa7GPTszoJxOiG5d_Q"
},
"snippet": {
"publishedAt": "2021-03-15T16:19:39Z",
"channelId": "UCp6iawa7GPTszoJxOiG5d_Q",
"title": "AndreoBee",
"description": "Welcome my ARS GAMING YouTube channel I am uploading full movies or shorts and how to download movies on my YouTube ...",
"thumbnails": {
"default": {
"url": "https://yt3.ggpht.com/Z2CC1zWbsD3k-z9cX0RlPBstKELZw0_sUgFPggIt-DhVkJWL8iJG8pCLizxr3StB8lbZX9PT7U8=s88-c-k-c0xffffffff-no-rj-mo"
},
"medium": {
"url": "https://yt3.ggpht.com/Z2CC1zWbsD3k-z9cX0RlPBstKELZw0_sUgFPggIt-DhVkJWL8iJG8pCLizxr3StB8lbZX9PT7U8=s240-c-k-c0xffffffff-no-rj-mo"
},
"high": {
"url": "https://yt3.ggpht.com/Z2CC1zWbsD3k-z9cX0RlPBstKELZw0_sUgFPggIt-DhVkJWL8iJG8pCLizxr3StB8lbZX9PT7U8=s800-c-k-c0xffffffff-no-rj-mo"
}
},
"channelTitle": "AndreoBee",
"liveBroadcastContent": "none",
"publishTime": "2021-03-15T16:19:39Z"
}
},
{
"kind": "youtube#searchResult",
"etag": "VHP4HsDcIiETPleAWN0cSC7fvbE",
"id": {
"kind": "youtube#channel",
"channelId": "UCWLY3sMMYHH3-9xQ2lhFj8g"
},
"snippet": {
"publishedAt": "2022-06-27T01:08:10Z",
"channelId": "UCWLY3sMMYHH3-9xQ2lhFj8g",
"title": "AndreoBee",
"description": "Hii guus i am Andreo I hope you like my videos and please support me guys thank you have a good day.",
"thumbnails": {
"default": {
"url": "https://yt3.ggpht.com/bHaKoA8QZnZSJXfmK7udtrzxmpCOOhpTHIoP_RQUD2KLv9qNXtLLKY_bewbaQGdfZGIPCcO9Ag=s88-c-k-c0xffffffff-no-rj-mo"
},
"medium": {
"url": "https://yt3.ggpht.com/bHaKoA8QZnZSJXfmK7udtrzxmpCOOhpTHIoP_RQUD2KLv9qNXtLLKY_bewbaQGdfZGIPCcO9Ag=s240-c-k-c0xffffffff-no-rj-mo"
},
"high": {
"url": "https://yt3.ggpht.com/bHaKoA8QZnZSJXfmK7udtrzxmpCOOhpTHIoP_RQUD2KLv9qNXtLLKY_bewbaQGdfZGIPCcO9Ag=s800-c-k-c0xffffffff-no-rj-mo"
}
},
"channelTitle": "AndreoBee",
"liveBroadcastContent": "none",
"publishTime": "2022-06-27T01:08:10Z"
}
},
{
"kind": "youtube#searchResult",
"etag": "01NvBqsdnNimHwnwHnGkeVOhJ-8",
"id": {
"kind": "youtube#channel",
"channelId": "UC3yVv5tuR3tBcVf97j33mQQ"
},
"snippet": {
"publishedAt": "2020-12-12T14:31:22Z",
"channelId": "UC3yVv5tuR3tBcVf97j33mQQ",
"title": "Andreobee",
"description": "Describe the game All best game for play on Android in 2GB ram and 3GB phone.",
"thumbnails": {
"default": {
"url": "https://yt3.ggpht.com/ytc/AL5GRJXval25GhQFpbqoFgQXvWBol5aqf4ZCOluMMOe-=s88-c-k-c0xffffffff-no-rj-mo"
},
"medium": {
"url": "https://yt3.ggpht.com/ytc/AL5GRJXval25GhQFpbqoFgQXvWBol5aqf4ZCOluMMOe-=s240-c-k-c0xffffffff-no-rj-mo"
},
"high": {
"url": "https://yt3.ggpht.com/ytc/AL5GRJXval25GhQFpbqoFgQXvWBol5aqf4ZCOluMMOe-=s800-c-k-c0xffffffff-no-rj-mo"
}
},
"channelTitle": "Andreobee",
"liveBroadcastContent": "none",
"publishTime": "2020-12-12T14:31:22Z"
}
},
{
"kind": "youtube#searchResult",
"etag": "B5TOx4aFyuua9eQVa-QBbKDjIng",
"id": {
"kind": "youtube#channel",
"channelId": "UCBR0QlbTtbT52oFmmDZIamw"
},
"snippet": {
"publishedAt": "2022-02-01T14:44:18Z",
"channelId": "UCBR0QlbTtbT52oFmmDZIamw",
"title": "AndreoBee",
"description": "",
"thumbnails": {
"default": {
"url": "https://yt3.ggpht.com/TfGkwtvsAvJdWnMOdRLRn2HKRvnVj872dxLZdZ_ghFwpF4SrY2BqBwJ3syc1V6HLL3LyJiSABQ=s88-c-k-c0xffffffff-no-rj-mo"
},
"medium": {
"url": "https://yt3.ggpht.com/TfGkwtvsAvJdWnMOdRLRn2HKRvnVj872dxLZdZ_ghFwpF4SrY2BqBwJ3syc1V6HLL3LyJiSABQ=s240-c-k-c0xffffffff-no-rj-mo"
},
"high": {
"url": "https://yt3.ggpht.com/TfGkwtvsAvJdWnMOdRLRn2HKRvnVj872dxLZdZ_ghFwpF4SrY2BqBwJ3syc1V6HLL3LyJiSABQ=s800-c-k-c0xffffffff-no-rj-mo"
}
},
"channelTitle": "AndreoBee",
"liveBroadcastContent": "none",
"publishTime": "2022-02-01T14:44:18Z"
}
},
{
"kind": "youtube#searchResult",
"etag": "t04J8v3aRhQhmFsyv54ddTXGDMQ",
"id": {
"kind": "youtube#channel",
"channelId": "UCBvdbv05Cb4Ulf_PlunMJtA"
},
"snippet": {
"publishedAt": "2021-08-12T06:59:40Z",
"channelId": "UCBvdbv05Cb4Ulf_PlunMJtA",
"title": "AndreoBee",
"description": "",
"thumbnails": {
"default": {
"url": "https://yt3.ggpht.com/S3J1R-s_P481lUpBULskSiQ0Pv-Z_iLnE_YrZ10OL1cydCFx6a1aOXvRJskMxY-gVpa8cqwuzQ=s88-c-k-c0xffffffff-no-rj-mo"
},
"medium": {
"url": "https://yt3.ggpht.com/S3J1R-s_P481lUpBULskSiQ0Pv-Z_iLnE_YrZ10OL1cydCFx6a1aOXvRJskMxY-gVpa8cqwuzQ=s240-c-k-c0xffffffff-no-rj-mo"
},
"high": {
"url": "https://yt3.ggpht.com/S3J1R-s_P481lUpBULskSiQ0Pv-Z_iLnE_YrZ10OL1cydCFx6a1aOXvRJskMxY-gVpa8cqwuzQ=s800-c-k-c0xffffffff-no-rj-mo"
}
},
"channelTitle": "AndreoBee",
"liveBroadcastContent": "none",
"publishTime": "2021-08-12T06:59:40Z"
}
},
{
"kind": "youtube#searchResult",
"etag": "dmhfSWj6r1CzXr4yopXkorSsVL0",
"id": {
"kind": "youtube#channel",
"channelId": "UCvXV0HSenVdIpdAuogNPuOQ"
},
"snippet": {
"publishedAt": "2022-11-02T12:01:42Z",
"channelId": "UCvXV0HSenVdIpdAuogNPuOQ",
"title": "AndreoBee",
"description": "PLZ SUB.....",
"thumbnails": {
"default": {
"url": "https://yt3.ggpht.com/NfH1RxDcGEbLe8cOoi7VwFz-dfUcAToV-84rag4x1CIQM8GSnZirguR5SI30M3bZnMP80tA=s88-c-k-c0xffffffff-no-rj-mo"
},
"medium": {
"url": "https://yt3.ggpht.com/NfH1RxDcGEbLe8cOoi7VwFz-dfUcAToV-84rag4x1CIQM8GSnZirguR5SI30M3bZnMP80tA=s240-c-k-c0xffffffff-no-rj-mo"
},
"high": {
"url": "https://yt3.ggpht.com/NfH1RxDcGEbLe8cOoi7VwFz-dfUcAToV-84rag4x1CIQM8GSnZirguR5SI30M3bZnMP80tA=s800-c-k-c0xffffffff-no-rj-mo"
}
},
"channelTitle": "AndreoBee",
"liveBroadcastContent": "none",
"publishTime": "2022-11-02T12:01:42Z"
}
},
{
"kind": "youtube#searchResult",
"etag": "c5Y3zUOZKiyJ-pI7mrLdi4WPegc",
"id": {
"kind": "youtube#channel",
"channelId": "UCmrR9PVxSVRLdp2HK_xP2yA"
},
"snippet": {
"publishedAt": "2021-09-13T06:16:47Z",
"channelId": "UCmrR9PVxSVRLdp2HK_xP2yA",
"title": "ANDREOBEE",
"description": "AndreoBee fan Club For business inquiries ANDREOBEEofficial.",
"thumbnails": {
"default": {
"url": "https://yt3.ggpht.com/CtsMEUKLJC5apm5bX7vEr_3g7nIJn-GfjLodWXLVLF-jFGfyXhFBTiLUvOU8l51e3kVPph5MUQ=s88-c-k-c0xffffffff-no-rj-mo"
},
"medium": {
"url": "https://yt3.ggpht.com/CtsMEUKLJC5apm5bX7vEr_3g7nIJn-GfjLodWXLVLF-jFGfyXhFBTiLUvOU8l51e3kVPph5MUQ=s240-c-k-c0xffffffff-no-rj-mo"
},
"high": {
"url": "https://yt3.ggpht.com/CtsMEUKLJC5apm5bX7vEr_3g7nIJn-GfjLodWXLVLF-jFGfyXhFBTiLUvOU8l51e3kVPph5MUQ=s800-c-k-c0xffffffff-no-rj-mo"
}
},
"channelTitle": "ANDREOBEE",
"liveBroadcastContent": "none",
"publishTime": "2021-09-13T06:16:47Z"
}
},
{
"kind": "youtube#searchResult",
"etag": "-lb0jmjUJcQsLJDGHH7KVs14-nU",
"id": {
"kind": "youtube#channel",
"channelId": "UCvqm9nqHYYehmq7FcorjS4Q"
},
"snippet": {
"publishedAt": "2021-01-13T05:54:00Z",
"channelId": "UCvqm9nqHYYehmq7FcorjS4Q",
"title": "AndreoBEE",
"description": "",
"thumbnails": {
"default": {
"url": "https://yt3.ggpht.com/ytc/AL5GRJUIkodf8aTOx2ZcNoyycMHQqgqNvehuXN_iz9Ii=s88-c-k-c0xffffffff-no-rj-mo"
},
"medium": {
"url": "https://yt3.ggpht.com/ytc/AL5GRJUIkodf8aTOx2ZcNoyycMHQqgqNvehuXN_iz9Ii=s240-c-k-c0xffffffff-no-rj-mo"
},
"high": {
"url": "https://yt3.ggpht.com/ytc/AL5GRJUIkodf8aTOx2ZcNoyycMHQqgqNvehuXN_iz9Ii=s800-c-k-c0xffffffff-no-rj-mo"
}
},
"channelTitle": "AndreoBEE",
"liveBroadcastContent": "none",
"publishTime": "2021-01-13T05:54:00Z"
}
}
]
}
注意:在这种情况下,我会接受第一个结果是我正在寻找的频道,但是,请注意,您可能会获得多个与频道名称匹配的频道。
要检测哪一个搜索结果与您正在寻找的频道匹配,您可以向 channel:list 端点发出请求 - 通过传递搜索结果的
channel_id
- 并查看它们的
customUrl
值和/或检查具有更多视频/订阅者/等的频道。
查看 我的答案 了解更多信息。
您可以尝试以下 node.js 脚本
var https = require('https');
var url = "https://www.youtube.com/@adnantoky"
https.get(url, function (res) {
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
let arr = data.split('channel_id=');
console.log("Channel ID:", arr[1].slice(0, 24));
});
}).on('error', function (e) {
console.log(e.message);
});
您可以使用
/search
Youtube API 获取频道 ID。改用此方法:
https://www.googleapis.com/youtube/v3/channels?part=id&forUsername=${channelUsername}&key=myapi
尝试使用此端点:
https://youtube.googleapis.com/youtube/v3/search?part=snippet&maxResults=25&q=${channelUsername}&type=channel&key=myapi