我正在为udemy做这个关于构建12个不同的角度2应用程序的课程,其中一个与Spotify Web API一起工作,我正在为它添加更多功能;
我已经学会了如何使用简单的GET请求
searchMusic(str:string, type='artist'){ this.searchUrl = 'https://api.spotify.com/v1/search?query='+str+'&offset=0&limit=20&type='+type+'&market=US'; return this._http.get(this.searchUrl) .map(res => res.json()); }该功能不需要auth密钥
但是要获取播放列表,我需要将auth键传递给请求,否则我会收到错误而不是Json格式的播放列表
如何将auth键附加到请求以消除错误?
谢谢!
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 how to work with simple GET request like
searchMusic(str:string, type='artist'){ this.searchUrl = 'https://api.spotify.com/v1/search?query='+str+'&offset=0&limit=20&type='+type+'&market=US'; return this._http.get(this.searchUrl) .map(res => res.json()); }That function requires no auth key
But to get a playlist I need to pass an auth key to the request, otherwise I get an error instead of a Json formatted playlist
How do you append the auth key to the request to get rid of the error?
Thanks!
最满意答案
您可以尝试此代码导入此文件
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()); }有关更多信息,请查看这些链接
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()); } }试试这个,如果上面没有工作
我希望这个能帮上忙
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
更多推荐
发布评论