向 Spotify API 端点发出请求失败

Put request to Spotify API endpoint failing

我正在尝试使用 Angular 的 HttpClient 向 Spotify Web API 端点发出放置请求,但请求失败,我不确定原因。看看下面的代码:

let headers = new HttpHeaders().set('Authorization', 'Bearer ' + this.accessToken);

const endpoint1 = 'https://api.spotify.com/v1/me/player/currently-playing';
this.httpClient.get(endpoint1, { headers: headers }).subscribe((data: Object) => {
  console.log(data);
});

const endpoint2 = 'https://api.spotify.com/v1/me/player/play';
this.httpClient.put(endpoint2, { headers: headers }).subscribe((data: Object) => {
  console.log(data);
});

get 请求工作正常,授权 header 在请求中设置并且按预期工作。但是,put 请求不起作用。在响应中,我收到一条错误消息 "No token provided"。此外,在 Chrome 开发人员工具中,网络选项卡显示带有授权 header 的获取请求,但放置请求没有授权 header.

我不确定这两个请求之间有什么不同会导致第二个错误,两个端点只需要授权 header 并且我已经确保访问令牌已通过访问这些端点所需的适当范围。

player/play 需要 "user-modify-playback-state" 范围,player/currently-playing 需要 "user-read-currently-playing" and/or "user-read-playback-state",我已经包括了所有三个。

可以在此处找到两个端点:

https://developer.spotify.com/documentation/web-api/reference/player/get-the-users-currently-playing-track/

https://developer.spotify.com/documentation/web-api/reference/player/start-a-users-playback/

为什么放置请求失败,我该如何解决?

您需要在header中传递token如下,

this.http.put('https://api.spotify.com/v1/me/player/play',
{
  context_uri: 'spotify:album:' + album.id
},
{
  headers: {
    'Authorization': 'Bearer ' + this.spotifyToken
  }
})
  .subscribe();