Angular:从 Spotify 请求访问 token 时出现 HTTP post 请求错误 "grant_type parameter is missing"

标签 angular http post authorization access-token

我正在尝试从 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/

相关文章:

javascript - 在 django 中获取列表值

caching - angular2 禁用浏览器缓存

javascript - 从 jquery call 调用 Angularjs HTTP 请求服务或选择 open

api - 使用 HTTP DELETE 方法取消正在进行的操作

angular - docker 上的 Nginx - 变化 : Accept-Encoding header is missing

java - 为什么 HttpClient 不发送我的 Cookie?

rest - 使用 GET 删除内容有什么问题?

Angular/TypeScript - 联合类型访问

angular - 类型错误 : [X] is not a function

angular - 使用具有反应形式的 Angular Material 垫选择时出错