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

yhqotfr8  于 2023-02-08  发布在  iOS
关注(0)|答案(2)|浏览(136)

我做了一个程序,用户提供一个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/索引. js:7如果(响应.数据.项目.长度=== 0){^
类型错误:无法读取getChannelId(/home/runner/ytcheck/index. js:7:27)处未定义(读取"长度")的属性

eqqqjvef

eqqqjvef1#

一种替代方法-使用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"
      }
    }
  ]
}

注意:在这种情况下,我将接受第一个结果是我正在寻找的通道,但是,请注意,您可能会得到多个与通道名称匹配的通道。
为了检测搜索结果中的哪一个与您正在寻找的频道匹配,您可以向频道:list端点发出请求-通过传递搜索结果的channel_id-并查看其customUrl值和/或检查具有更多视频/订阅者/等的频道。
查看my answer了解更多信息。

wpx232ag

wpx232ag2#

您可以使用/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

相关问题