Angular 角度:HTTP post请求错误“;grant“缺少U类型参数”;从Spotify请求访问令牌时
我正试图从Spotify获取访问令牌,这样我就可以检索公开可用的信息(曲目、专辑等),但当我发出POST请求时,返回的响应告诉我grant_类型缺失。下面是我正在使用的代码:Angular 角度:HTTP post请求错误“;grant“缺少U类型参数”;从Spotify请求访问令牌时,angular,http,post,authorization,access-token,Angular,Http,Post,Authorization,Access Token,我正试图从Spotify获取访问令牌,这样我就可以检索公开可用的信息(曲目、专辑等),但当我发出POST请求时,返回的响应告诉我grant_类型缺失。下面是我正在使用的代码: import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; import { HttpClient, HttpHeaders } from '@angular/common/http'; //clientId and cli
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,错误:{错误:不支持的grant_类型,错误描述:grant_类型参数丢失}我遇到了相同的问题,并通过使用以下字符串替换body对象修复了该问题: “授予\类型=客户端\凭据”。因此,在您的情况下,它将是:
body = 'grant_type=client_credentials';
我不知道为什么它是这样工作的,但对一个对象却不行。我也有同样的问题,并通过用以下字符串替换body对象来修复它: “授予\类型=客户端\凭据”。因此,在您的情况下,它将是:
body = 'grant_type=client_credentials';
我不知道为什么它是这样工作的,但不是用一个物体