我正在尝试从 Spotify 获取访问 token ,这样我就可以检索公开可用的信息(轨道、专辑等),但是当我发出 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: 不支持的 grant_type, error_description: grant_type 参数丢失}
最佳答案
我遇到了同样的问题,并通过用以下字符串替换 body 对象来修复它: 'grant_type=client_credentials'。所以在你的情况下它将是:
body = 'grant_type=client_credentials';
我不知道为什么它会这样工作,但对于对象却不起作用。
关于Angular:从 Spotify 请求访问 token 时出现 HTTP post 请求错误 "grant_type parameter is missing",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61218589/