【问题标题】:Put request to Spotify API endpoint failing向 Spotify API 端点发出请求失败
【发布时间】:2018-12-10 21:57:10
【问题描述】:

我正在尝试使用 Angular 的 HttpClient 向 Spotify Web API 端点发出 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 标头。

我不确定这两个请求之间有什么不同会导致第二个错误,两个端点只需要 Authorization 标头,并且我已确保已为访问令牌提供了访问这些所需的适当范围端点。

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

这两个端点可以在这里找到:

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 请求失败,我该如何解决?

【问题讨论】:

    标签: angular spotify angular-httpclient


    【解决方案1】:

    你需要在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();
    

    【讨论】:

    • 哦,对了,我把 options 对象放入了 put 函数的错误参数中。
    • 如果回答有帮助就点赞
    猜你喜欢
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 2022-01-10
    • 2011-11-08
    • 2021-06-08
    相关资源
    最近更新 更多