如何使用频道名称 URL 获取 YouTube 频道 ID?

问题描述 投票:0回答:4

我制作了一个程序,用户提供 YouTube 频道 URL,作为回报,我将向用户显示其频道 ID。

如果有人输入如下网址: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:无法读取未定义的属性(读取“长度”) 在 getChannelId (/home/runner/ytcheck/index.js:7:27)

javascript axios youtube-data-api
4个回答
1
投票

您可以尝试以下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);
});

1
投票

另一种选择 - 使用 YouTube 数据 API - 可能是:

  • 使用 search:list 端点来搜索与条件匹配的频道 - 在本例中为频道的名称/句柄。

示例:

搜索名为“AndreoBee”的频道 - 您将在代码中的

channelUsername
变量中获取此值:

网址:

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
评估和/或检查拥有更多视频/订阅者/等的频道。

查看我的回答以获取更多信息。


0
投票

要使用频道名称 URL 获取 YouTube 频道 ID,您可以使用名为“YouTube 频道 ID 提取器”的在线工具。请按照以下步骤操作:

  1. 转至您首选的搜索引擎并输入“YouTube 频道 ID 提取器。
  2. 从搜索结果中选择任何可靠的网站。
  3. 进入网站后,将 YouTube 频道链接粘贴到指定的位置 区域。
  4. 然后该工具将提取并显示频道 与所提供的 URL 关联的 ID。

这个过程很简单,不需要任何技术技能。只需按照步骤操作,您很快就会获得频道 ID。


-1
投票

您可以使用

/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
© www.soinside.com 2019 - 2024. All rights reserved.