Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度:HTTP post请求错误“;grant“缺少U类型参数”;从Spotify请求访问令牌时_Angular_Http_Post_Authorization_Access Token - Fatal编程技术网

Angular 角度:HTTP post请求错误“;grant“缺少U类型参数”;从Spotify请求访问令牌时

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

我正试图从Spotify获取访问令牌,这样我就可以检索公开可用的信息(曲目、专辑等),但当我发出POST请求时,返回的响应告诉我grant_类型缺失。下面是我正在使用的代码:

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';
我不知道为什么它是这样工作的,但不是用一个物体