Spotifyの検索APIで日本語の結果が返されないときの対処法

Spotifyの検索APIで検索をした際、コンソールとcurlでアーティスト名やトラック名が日本語で返ってこないことがあります。

例えば「水曜日のカンパネラ」で検索をしてみると、nameのところに「水曜日のカンパネラ」と日本語で文字が返ってきます。

https://developer.spotify.com/console/get-search-item/?q=%E6%B0%B4%E6%9B%9C%E6%97%A5%E3%81%AE%E3%82%AB%E3%83%B3%E3%83%91%E3%83%8D%E3%83%A9&type=artist&market=&limit=&offset=

{

"artists": {
"href": "https://api.spotify.com/v1/search?query=%E6%B0%B4%E6%9B%9C%E6%97%A5%E3%81%AE%E3%82%AB%E3%83%B3%E3%83%91%E3%83%8D%E3%83%A9&type=artist&market=JP&offset=0&limit=20",
"items": [
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/0bp9CvkjQYhUJvWRHjFRWI"
},
"followers": {
"href": null,
"total": 74886
},
"genres": [
"j-dance",
"j-rock",
"shibuya-kei"
],
"href": "https://api.spotify.com/v1/artists/0bp9CvkjQYhUJvWRHjFRWI",
"id": "0bp9CvkjQYhUJvWRHjFRWI",
"images": [
{
"height": 640,
"url": "https://i.scdn.co/image/7a4c7ce6ba3f6026110b37f6b6df697520c85dbf",
"width": 640
},
{
"height": 320,
"url": "https://i.scdn.co/image/802457a9e1c49d765b5bbc11da67c8150c371758",
"width": 320
},
{
"height": 160,
"url": "https://i.scdn.co/image/4c21f512b40e4614b74d1adb31ac9fc0e45d9a76",
"width": 160
}
],
"name": "水曜日のカンパネラ",
"popularity": 49,
"type": "artist",
"uri": "spotify:artist:0bp9CvkjQYhUJvWRHjFRWI"
}
],
"limit": 20,
"next": null,
"offset": 0,
"previous": null,
"total": 1
}
}

これに対し、curlで叩いてみると、nameの箇所が"WEDNESDAY CAMPANELLA"と返ってきます。

curl -X "GET" "https://api.spotify.com/v1/search?q=%E6%B0%B4%E6%9B%9C%E6%97%A5%E3%81%AE%E3%82%AB%E3%83%B3%E3%83%91%E3%83%8D%E3%83%A9&type=artist" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer [Access Token]"

{

"artists": {
"href": "https://api.spotify.com/v1/search?query=%E6%B0%B4%E6%9B%9C%E6%97%A5%E3%81%AE%E3%82%AB%E3%83%B3%E3%83%91%E3%83%8D%E3%83%A9&type=artist&market=JP&offset=0&limit=20",
"items": [
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/0bp9CvkjQYhUJvWRHjFRWI"
},
"followers": {
"href": null,
"total": 74886
},
"genres": [
"j-dance",
"j-rock",
"shibuya-kei"
],
"href": "https://api.spotify.com/v1/artists/0bp9CvkjQYhUJvWRHjFRWI",
"id": "0bp9CvkjQYhUJvWRHjFRWI",
"images": [
{
"height": 640,
"url": "https://i.scdn.co/image/7a4c7ce6ba3f6026110b37f6b6df697520c85dbf",
"width": 640
},
{
"height": 320,
"url": "https://i.scdn.co/image/802457a9e1c49d765b5bbc11da67c8150c371758",
"width": 320
},
{
"height": 160,
"url": "https://i.scdn.co/image/4c21f512b40e4614b74d1adb31ac9fc0e45d9a76",
"width": 160
}
],
"name": "WEDNESDAY CAMPANELLA",
"popularity": 49,
"type": "artist",
"uri": "spotify:artist:0bp9CvkjQYhUJvWRHjFRWI"
}
],
"limit": 20,
"next": null,
"offset": 0,
"previous": null,
"total": 1
}
}

このnameを日本語で返したい場合、Accept-Languageヘッダーをリクエスト時に設定すれば良いようです。

先程のcurlに -H 'Accept-Language: ja;q=1'をつけて送信してみます。

curl -X "GET" "https://api.spotify.com/v1/search?q=%E6%B0%B4%E6%9B%9C%E6%97%A5%E3%81%AE%E3%82%AB%E3%83%B3%E3%83%91%E3%83%8D%E3%83%A9&type=artist" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer [Access Token]" -H 'Accept-Language: ja;q=1'

すると、webコンソールと同様の結果が返ってきます。

{

"artists": {
"href": "https://api.spotify.com/v1/search?query=%E6%B0%B4%E6%9B%9C%E6%97%A5%E3%81%AE%E3%82%AB%E3%83%B3%E3%83%91%E3%83%8D%E3%83%A9&type=artist&market=JP&offset=0&limit=20",
"items": [
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/0bp9CvkjQYhUJvWRHjFRWI"
},
"followers": {
"href": null,
"total": 74886
},
"genres": [
"j-dance",
"j-rock",
"shibuya-kei"
],
"href": "https://api.spotify.com/v1/artists/0bp9CvkjQYhUJvWRHjFRWI",
"id": "0bp9CvkjQYhUJvWRHjFRWI",
"images": [
{
"height": 640,
"url": "https://i.scdn.co/image/7a4c7ce6ba3f6026110b37f6b6df697520c85dbf",
"width": 640
},
{
"height": 320,
"url": "https://i.scdn.co/image/802457a9e1c49d765b5bbc11da67c8150c371758",
"width": 320
},
{
"height": 160,
"url": "https://i.scdn.co/image/4c21f512b40e4614b74d1adb31ac9fc0e45d9a76",
"width": 160
}
],
"name": "水曜日のカンパネラ",
"popularity": 49,
"type": "artist",
"uri": "spotify:artist:0bp9CvkjQYhUJvWRHjFRWI"
}
],
"limit": 20,
"next": null,
"offset": 0,
"previous": null,
"total": 1
}
}

なぜwebコンソールで日本語表示ができているかというと、ブラウザの言語設定を日本語にしてAccept-Languageヘッダーが日本語で送信されたため、結果も日本語で表示されるようです。