在Express.js上使用Axios向Spotify API发出POST请求时出现错误400

蒂亚戈

在Express后端服务器上使用axios发出发布请求时,我试图从Spotify API中检索访问令牌。到目前为止,我一直没有成功。我收到以下错误:

数据:{错误:“ unsupported_grant_type”,错误说明:“ grant_type必须是client_credentials,authorization_code或refresh_token”}}}

我已经尝试将'grant_type'的'data'属性更改为'params',但仍然无法正常工作。任何建议都会有所帮助。

const express = require('express');
const axios = require('axios');
const dotenv = require('dotenv');
dotenv.config();
const app = express();
const port = 3000;

const client_id = process.env.CLIENT_ID;
const client_secret = process.env.CLIENT_SECRET;

app.get('/spotify-authorization', (req, res) => {
  axios({
    method: 'post',
    url: 'https://accounts.spotify.com/api/token',
    data: {
      grant_type: 'client_credentials'
    },
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      Authorization:
        'Basic ' +
        Buffer.from(client_id + ':' + client_secret).toString('base64')
    }
  })
    .then(response => {
      console.log(response.data);
    })
    .catch(error => {
      console.log(error);
    });

  res.send('successful response received!');
});

app.listen(port, () => console.log(`Express app listening on port ${port}!`));

我希望能够从Spotify API的响应中检索访问令牌。请帮忙!

1565986223

axios文档:By default, axios serializes JavaScript objects to JSON. To send data in the application/x-www-form-urlencoded format instead, you can use one of the following options.

对于Nodejs,您可以使用以下querystring模块:

var querystring = require('querystring');
axios.post('http://something.com/', querystring.stringify({ foo: 'bar' }));

因此,您可以尝试 data: querystring.stringify({ grant_type: 'client_credentials' })

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Rest Assured发出POST请求时出现内部服务器错误

使用`django.test.client`时出现400错误请求

从Angular JS使用Web API令牌认证时出现错误请求(400)

如何使用express.js向本地json文件发出GET请求?

400-使用Web API在CRM中保存“仅日期”字段时出现错误请求

向Express Server发出多个AngularJS Post请求?服务器在第二次发布请求时崩溃

使用JSON正文发送POST请求时出现400响应

向Google Maps API发出GET请求,返回错误请求400错误-javascript

使用Nodemailer在React和Express中发出发布请求时出现问题

假客户端向微服务发出POST / PUT请求时出现错误403

通过Faraday向GitHub API发出请求时,我不断收到错误的状态代码(400、401、403)

使用Express发出POST请求时出错

无法使用Express API发出POST或GET请求

使用Axios发出发布请求时出现网络错误

在Swift中向本地服务器发出发布请求时出现400错误

尝试向API发出多个请求时出现TypeScript可观察到的错误(Angular,TypeScript,RxJS)

400-在python下使用pushalot api时出现错误请求

向Google gmail API发出令牌请求时,请求400错误

使用Pushbullet Ephemerals时出现400(错误请求)错误

使用AngularJS在Youtube API更新上出现400错误请求

无法对 api 执行 POST,使用 fetch 时出现错误 400

在 Volley 中实现 POST 请求时出现错误代码 400,但在使用 OkHttp 时则不会

使用 Node Js 和 express 调用来自 rest api 的 post 请求时出错。请求数据变空

当我向 api 服务器发出 post 请求时收到 404 错误

通过 Nodejs 上的 npm 请求模块发出 POST 请求时出现 CERT_HAS_EXPIRED 错误,但证书有效

向具有 CSRF 保护的 Ruby on Rails 应用程序发出 POST 请求时出现 404 错误

从 React 使用 API POST 到 ASP.net 控制器时总是出现 400 错误

尝试向pypi api发出请求时出现403错误

向 django API 发出 POST 请求时出错