I'm currently working my way through some tutorials for Angular2. Unfortunately my tutorial doesn't cover that the Spotify API needs an authorization by now. I already set up an application in the Spotify Dev Corner. My code for my Angular2-Service looks like this
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class SpotifyService {
private searchUrl: string;
private token: string;
private client_id = 'xxxx';
private client_secret = 'yyyy';
private redirect_uri = 'http://localhost:4200';
private responseType = 'code';
constructor(private _http:Http){
}
authorize(){
return this._http
.get('https://accounts.spotify.com/authorize?client_id=' + this.client_id + '&client_secret=' + this.client_secret + '&redirect_uri=' + this.redirect_uri + '&response_type=' + this.responseType)
.map(res => res.json());
}
}
And the constructor in my component looks like this
constructor(private _spotifyService:SpotifyService) {
this._spotifyService.authorize().subscribe(res => {
console.log(res);
});
}
Now my problem is that when my app runs, I get an error with the cross origins policy that confuses me a little and I'm not really sure where to set the header correctly.
XMLHttpRequest cannot load https://accounts.spotify.com/authorize?client_id=xxxx&client_secret=yyyy&redirect_uri=http://localhost:4200&response_type=code. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.
I have added the redirect_uri to my spotify app in their dev corner. Maybe you could help out. Thanks in advance and
Greetings Chris