【问题标题】:Adding spotify track to playlist returns 401将 Spotify 曲目添加到播放列表会返回 401
【发布时间】:2020-01-30 05:08:02
【问题描述】:

我正在 Angular 中创建一个项目,并在其中使用 Spotify API。但是,当我想将歌曲添加到我的播放列表时,我会收到 401 响应。

addTrackToPlaylist(songID: string, authToken: string) {
let headers = new HttpHeaders();
headers = headers.append('Authorization', 'Bearer ' + authToken);
this.searchUrl = 'https://api.spotify.com/v1/playlists/6VO88OLaELcJxNhwwIgxam/tracks?uris=spotify:track:' + songID;

return this.http.post(this.searchUrl, { headers })
.pipe(map((response: any) => response));
}

回复:

error: {status: 401, message: "No token provided"}

但是令牌已设置?

【问题讨论】:

    标签: angular typescript spotify


    【解决方案1】:

    post 的第二个参数用于有效负载,而不是标头。第三个参数是httpOptions。您当前正在正文中设置授权标头。

    const body = {};
    const httpOptions = {
      headers: new HttpHeaders({
        'Authorization': `Bearer ${authToken}`
      });
    };
    
    return this.http.post(this.searchUrl, body, httpOptions)
    .pipe(map((response: any) => response));
    

    https://angular.io/guide/http

    【讨论】:

      猜你喜欢
      • 2017-06-16
      • 2012-09-22
      • 1970-01-01
      • 2012-12-20
      • 2014-01-28
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多