【问题标题】:Angular: HTTP post request error "grant_type parameter is missing" when requesting access token from SpotifyAngular:从 Spotify 请求访问令牌时,HTTP 发布请求错误“grant_type 参数丢失”
【发布时间】: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


    【解决方案1】:

    我遇到了同样的问题,并通过将 body 对象替换为以下字符串来修复它: 'grant_type=client_credentials'。所以你的情况是:

    body = 'grant_type=client_credentials';
    

    我不知道为什么它会这样工作,但不是一个对象。

    【讨论】:

      猜你喜欢
      • 2017-07-20
      • 2019-09-16
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 2012-09-06
      • 1970-01-01
      相关资源
      最近更新 更多