import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';
/**
* SpotifyService works querying the Spotify Web API
* https://developer.spotify.com/web-api/
*/
@Injectable()
export class SpotifyService {
static BASE_URL = 'https://api.spotify.com/v1';
constructor(private http: Http) {
}
query(URL: string, params?: Array<string>): Observable<any[]> {
let queryURL = `${SpotifyService.BASE_URL}${URL}`;
if (params) {
queryURL = `${queryURL}?${params.join('&')}`;
}
return this.http.request(queryURL).map((res: any) => res.json());
}
search(query: string, type: string): Observable<any[]> {
return this.query(`/search`, [
`q=${query}`,
`type=${type}`
]);
}
searchTrack(query: string): Observable<any[]> {
return this.search(query, 'track');
}
getTrack(id: string): Observable<any[]> {
return this.query(`/tracks/${id}`);
}
getArtist(id: string): Observable<any[]> {
return this.query(`/artists/${id}`);
}
getAlbum(id: string): Observable<any[]> {
return this.query(`/albums/${id}`);
}
}
export const SPOTIFY_PROVIDERS: Array<any> = [
{ provide: SpotifyService, useClass: SpotifyService }
];
That is my spotify.service.ts file. The console is giving me this error:
Failed to load resource: the server responded with a status of 401 (Unauthorized) core.es5.js:1084 ERROR Responseheaders: Headers {_headers: Map(1), _normalizedNames: Map(1)}ok: falsestatus: 401statusText: "Unauthorized"type: 2url: "https://api.spotify.com/v1/search?q=eminem&type=track"_body: "{↵ "error": {↵ "status": 401,↵ "message": "No token provided"↵ }↵}"proto: Bodyconstructor: ƒ Response(responseOptions)toString: ƒ ()proto: Object defaultErrorLogger @ core.es5.js:1084
Am I to assume this is because I don't have an API Key? This is the example code provided straight from the book. If I need a key how would I implement it?