【发布时间】:2020-09-22 18:29:25
【问题描述】:
我正在尝试从 Spotify 获取访问令牌,这样我就可以检索公开可用的信息(曲目、专辑等),但是当我发出 POST 请求时,响应回来告诉我 grant_type 丢失 这是我正在使用的代码:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';
//clientId and clientSecret declared up here
tokenUrl: string = "https://accounts.spotify.com/api/token";
idAndSecret: string = btoa(this.clientId + ":" + this.clientSecret);
constructor(private httpClient: HttpClient) { }
body = {
'grant_type': "client_credentials",
};
options = {
headers: new HttpHeaders({
'Authorization': 'Basic '.concat(this.idAndSecret),
'Content-Type': 'application/x-www-form-urlencoded',
})
};
getAccessToken(): string{
this.httpClient.post(this.tokenUrl, this.body, this.options).subscribe(
response => {
console.log(response);
}
);
return this.token;
}
HttpErrorResponse 返回状态:400 和错误:{error: unsupported grant_type, error_description: grant_type parameter is missing}
【问题讨论】:
标签: angular http post authorization access-token