我同意这似乎违反直觉,但我已通过 Youtube 开发人员关系团队向我保证,给定频道名称的搜索方法的第一个结果将始终返回正确的频道项目(和 id)。
确保在搜索中保留 type = "channel",并且可以通过在调用中包含 "maxResults=1" 将结果集限制为 1。
例如,以下调用:
https://www.googleapis.com/youtube/v3/search?part=id%2Csnippet&maxResults=1&q=thejntest&type=channel&key={YOUR_API_KEY}
返回:
{
"kind": "youtube#searchListResponse",
"etag": "\"4IfXb8Dc78bqDKapqHxgOpqGpzM/lKqCVcHLORctmp6mp9sgSil_W0w\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"4IfXb8Dc78bqDKapqHxgOpqGpzM/UoEAOMl45xX5SIWjDGxyS-74WKE\"",
"id": {
"kind": "youtube#channel",
"channelId": "UCWLptbzZMaxQ7doUzgjMg_A"
},
"snippet": {
"publishedAt": "2013-04-23T08:28:54.000Z",
"channelId": "UCWLptbzZMaxQ7doUzgjMg_A",
"title": "TheJNtest",
"description": "What a wonderful testing world it is!",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/i/WLptbzZMaxQ7doUzgjMg_A/1.jpg"
},
"medium": {
"url": "https://i.ytimg.com/i/WLptbzZMaxQ7doUzgjMg_A/mq1.jpg"
},
"high": {
"url": "https://i.ytimg.com/i/WLptbzZMaxQ7doUzgjMg_A/hq1.jpg"
}
},
"channelTitle": "TheJNtest"
}
}
]
}
其中“UCWLptbzZMaxQ7doUzgjMg_A”是您的频道 ID。