首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Axios拦截器刷新令牌

Axios拦截器刷新令牌
EN

Stack Overflow用户
提问于 2021-02-22 13:15:50
回答 1查看 50关注 0票数 1

我正在尝试使用Axios进行多个应用程序接口调用,我正在创建一个服务类来实现它,我得到了一个400代码状态,带有'invalid_client‘头。这就是我如何构建我的Service类(我正在尝试对spotify api进行api调用)

代码语言:javascript
复制
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'),
    }
  );
}

EN

回答 1

Stack Overflow用户

发布于 2021-02-22 13:32:33

问题似乎是您没有正确添加Authorization标头值,因为您的行中有一个拼写错误:

代码语言:javascript
复制
      config.header.Authorization = `Bearer ${auth.data.access_token}`;

应该是headers,即:

代码语言:javascript
复制
      config.headers.Authorization = `Bearer ${auth.data.access_token}`;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66310350

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档