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

前端 未结 2 1982
迷失自我
迷失自我 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:37

    you can try this code import this file

    import { Http, Response, Headers, RequestOptions } from '@angular/http';
    
       searchMusic(str:string, type='artist'){
            let headers = new Headers({ 'Content-Type': 'application/json' },{'Authorization:'add_your_token_here'}); // ... Set content type to JSON
            let options = new RequestOptions({ headers: headers }); // Create a request option
            this.searchUrl = 'https://api.spotify.com/v1/search?query='+str+'&offset=0&limit=20&type='+type+'&market=US';
            return this._http.get(this.searchUrl, options)
                .map(res => res.json());
             }
    

    for more information check these links

    https://scotch.io/tutorials/angular-2-http-requests-with-observables https://angular.io/docs/ts/latest/guide/server-communication.html#!#override-default-request-options

    import { Injectable } from '@angular/core';
    import { Http, Headers } from '@angular/http';
    
    @Injectable()
    export class PfService {
      constructor(private http: Http) {}
    
      getProfile() {
        let headers = new Headers();
        headers.append('Content-Type', 'application/json');
        let authToken = localStorage.getItem('auth_token');
        headers.append('Authorization', `Bearer ${authToken}`);
    
        return this.http
          .get('/profile', { headers })
          .map(res => res.json());
      }
    }
    

    try this if above not work

    i hope this will help

提交回复
热议问题