Get an users playlist with Spotify API (how to add access token in http request angular 2?)

前端 未结 2 1975
迷失自我
迷失自我 2021-01-25 15:03

I\'m doing this course in udemy about building 12 different angular 2 apps, and one of them works with Spotify Web API and I\'m adding more features to it;

I\'ve learn h

2条回答
  •  囚心锁ツ
    2021-01-25 15:21

    You may find it helpful if other answers wouldn't work...

    import {Injectable} from '@angular/core';
    import {Http, Headers} from '@angular/http';
    import 'rxjs/add/operator/map';
    
    @Injectable()
    
    export class SearchService {
      private searchUrl: string;
    
      constructor (private _http: Http) {
    
      }
    
      searchMusic(str:string, type='artist'){
          let headers = new Headers();
          let authToken = 'Your OAuth Token Goes Here';
          headers.append('Authorization', 'Bearer '+authToken);
          this.searchUrl = 'https://api.spotify.com/v1/search?q='+str+'&offset=0&limit=20&type='+type+'&market=US';
          return this._http.get(this.searchUrl, { headers })
            .map(res => res.json());
      }
    }
    

    You can also generate OAuth Token from below link:

    https://developer.spotify.com/web-api/console/get-search-item/

提交回复
热议问题