简体   繁体   English

以下代码有什么问题?

[英]What's wrong with the below code?

I am working on a sample Angular2 application which uses Spotify API to get data. 我正在使用Angular2应用程序示例,该应用程序使用Spotify API来获取数据。 When I run the code, I'm getting response Error in console as "Invalid Access Token". 运行代码时,控制台中的响应错误为“无效的访问令牌”。 I have provided the correct access token, still the error persists, I'm not getting how to resolve it and what's wrong I'm doing. 我提供了正确的访问令牌,但错误仍然存​​在,我没有得到如何解决它的信息,我在做什么错。

import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()
export class SpotifyService {
  private searchUrl: string;   

  constructor(private _http: Http) { }

  searchMusic(str: string, type = 'artist') {
    const access_token = '<My Access Token Here>';
    const headers = new Headers({ 'Authorization': 'Bearer ' + access_token });
    this.searchUrl = 'https://api.spotify.com/v1/search?query='+str+'&offset=0&limit=20&type='+type+'&market=US';

    return this._http
      .get(this.searchUrl, { headers })
      .map(res => res.json());
  }
}

Error Screenshot : 错误截图: 在此处输入图片说明

You can check by adding one of the content-type in headers 您可以通过在标头中添加内容类型之一来进行检查

var headers: Headers = new Headers({'Authorization': 'Bearer ' + access_token, 'Content-Type': 'application/x-www-form-urlencoded' });

OR 要么

var headers: Headers = new Headers({'Authorization': 'Bearer ' + access_token, 'Content-Type': 'application/json' });

were you able to get a valid response by giving your access token in their api console? 通过在他们的api控制台中提供访问令牌,您能否获得有效的响应?

https://developer.spotify.com/web-api/console/get-search-item/?q=tania+bowra&type=artist https://developer.spotify.com/web-api/console/get-search-item/?q=tania+bowra&type=artist

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM