导入
搜索 Spotify 资源
学习如何搜索关于艺术家、播放列表、专辑、节目或剧集的信息。
搜索端点允许我们搜索任何艺术家、专辑、歌曲、播客或节目相关信息。该端点的基础URI为 https://api.spotify.com/v1/search,返回Spotify上可获取的信息。此端点提供可用于其他需要Spotify ID的端点的Spotify ID。
| 查询参数 | 类别 | 类型 | 描述 |
|---|---|---|---|
| q | 必需 | 字符串 | 要通过此端点搜索的查询关键词。 |
| type | 必需 | 字符串 | 要搜索的类别。可能的选项包括:album(专辑)、artist(艺术家)、playlist(播放列表)、track(歌曲)、show(节目)、episode(单集)。也可以通过逗号(,)分隔多个类别。 |
| include_external | 可选 | 字符串 | 决定用户是否可以播放外部托管的音频。该参数唯一可能的值是 audio,表示允许用户播放外部托管的音频。默认情况下,用户不允许这样做。 |
| limit | 可选 | 整数 | 限制响应中返回的项目数量。取值范围为0到50,默认值为20。 |
| market | 可选 | 字符串 | 用于获取特定国家内容的ISO 3166-1 alpha-2国家代码。例如,us代表美国,es代表西班牙,fr代表法国。如果在请求头中发送了授权码访问令牌,则用户的国家将优先于本参数指定的国家。 |
| offset | 可选 | 整数 | 用于偏移响应中的第一个项目。 |
1const endpointUrl = new URL('https://api.spotify.com/v1/search'); 2 3const queryParameters = new URLSearchParams({ 4 q: 'metallica', 5 type: 'artist' 6}); 7headerParameters = { 8 'Content-Type': 'application/json', 9 'Authorization': 'Bearer {{CLIENT_CREDENTIALS_ACCESS_TOKEN}}' 10} 11 12const options = { 13 method: 'GET', 14 headers: headerParameters 15}; 16 17async function searchArtist() { 18 try { 19 endpointUrl.search = queryParameters; 20 const response = await fetch(endpointUrl, options); 21 22 printResponse(response); 23 } 24 catch (error) { 25 printError(error); 26 } 27} 28 29searchArtist();
1const endpointUrl = new URL('https://api.spotify.com/v1/search'); 2 3const queryParameters = new URLSearchParams({ 4 q: 'metallica', 5 type: 'artist' 6}); 7headerParameters = { 8 'Content-Type': 'application/json', 9 'Authorization': 'Bearer {{AUTHORIZATION_CODE_ACCESS_TOKEN}}' 10} 11 12const options = { 13 method: 'GET', 14 headers: headerParameters 15}; 16 17async function searchArtistWithUserToken() { 18 try { 19 endpointUrl.search = queryParameters; 20 const response = await fetch(endpointUrl, options); 21 22 printResponse(response); 23 } 24 catch (error) { 25 printError(error); 26 } 27} 28 29searchArtistWithUserToken();
| 响应字段 | 类型 | 描述 |
|---|---|---|
| genres | 字符串数组 | 包含该艺术家参与过的音乐流派的数组。 |
| name | 字符串 | 艺术家的名称。 |
| popularity | 整数 | 表示艺术家在Spotify上的受欢迎程度,范围从0到100,100为最热门。 |
| images | 对象 [数组] | 包含艺术家图像信息。数组中的每个对象包含三个元素,分别提供图像的URL、高度和宽度。 |
| id | 字符串 | 艺术家的Spotify ID。 |
| next | 字符串 | 下一页的链接。 |
| previous | 字符串 | 上一页的链接。 |
| total | 整数 | 与本次查询匹配的项目总数(此例中为艺术家总数)。 |