我正在尝试使用Axios进行多个应用程序接口调用,我正在创建一个服务类来实现它,我得到了一个400代码状态,带有'invalid_client‘头。这就是我如何构建我的Service类(我正在尝试对spotify api进行api调用)
import axios from "axios";
import { Credentials } from './config';
export default class SpotifyService {
constructor() {
const spotify = Credentials();
this.service = axios.create({
baseUrl: "https://api.spotify.com/v1/browse",
});
this.service.interceptors.request.use(async (config) => {
const auth = await authentication();
config.headers.Authorization = `Bearer ${auth.data.access_token}`;
});
}
getCategories = () => {
return this.service.get("/categories?limit=6");
};
}
async function authentication() {
const response = await axios.post(
"https://accounts.spotify.com/api/token",
"grant_type=client_credentials",
{
"Content-Type": "application/x-www-form-urlencoded",
Authorization:
"Basic " + btoa('clientId:clientSecret'),
}
);
}
发布于 2021-02-22 13:32:33
问题似乎是您没有正确添加Authorization标头值,因为您的行中有一个拼写错误:
config.header.Authorization = `Bearer ${auth.data.access_token}`;应该是headers,即:
config.headers.Authorization = `Bearer ${auth.data.access_token}`;https://stackoverflow.com/questions/66310350
复制相似问题