使用Spotify API获取用户播放列表(如何在http请求角度2中添加访问令牌?)(Get an users playlist with Spotify API (how to add acces

编程入门 行业动态 更新时间:2024-10-28 18:25:11
使用Spotify API获取用户播放列表(如何在http请求角度2中添加访问令牌?)(Get an users playlist with Spotify API (how to add access token in http request angular 2?))

我正在为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

更多推荐

本文发布于:2023-04-28 08:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330924.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:令牌   播放列表   角度   用户   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!