首页 文章

将请求置于Spotify API endpoints 失败

提问于
浏览
0

我正在尝试使用Angular的HttpClient向Spotify Web API endpoints 发出put请求,但请求失败,我不知道为什么 . 看一下下面的代码:

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请求工作正常,Authorization标头在请求中设置,它按预期工作 . 然而,put请求不起作用 . 在响应中,我收到一条错误消息“无提供令牌” . 此外,在Chrome开发人员工具中,网络标签显示带有Authorization标头的get请求,但put请求没有Authorization标头 .

我不确定导致第二个错误的两个请求之间有什么不同,两个 endpoints 只需要Authorization标头,并且我已经确保访问令牌具有访问这些 endpoints 所需的适当范围 .

播放器/播放需要“用户修改播放状态”范围和播放器/当前播放需要“用户读取当前正在播放”和/或“用户读取播放状态”,我已经包括所有三 .

这两个 endpoints 可以在这里找到:

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/

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

1 回答

  • 0

    您需要在标头中传递令牌,如下所示,

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

相关问题