NodeJS {“error”:“invalid_client”}客户端凭证错误spotify与快速

watbbzwu  于 2022-11-22  发布在  Node.js
关注(0)|答案(1)|浏览(104)

我正在spotify上开发一个api。我想检索客户端凭据。我在 Jmeter 板上设置了我的应用程序。我的client_id和secret是正确的。但当我尝试检索此客户端凭据时,最后出现了相同的错误:“error”:“invalid_client”我在网上找我的问题,但没有人对应我的问题。下面是我的代码:`

const express = require("express");
const path = require("path");
const cors = require("cors");
const fetch = (...args) =>
    import('node-fetch').then(({default: fetch}) => fetch(...args));
  
const request = "https://accounts.spotify.com/api/token";

const code = Buffer.from(client_id + ":" + client_secret).toString("base64");

const app = express();
const optionsTOKEN = {
  method: "POST",
  body: "grant_type=client_credentials",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
    "Authorization": "Basic" +code
  },
  // json: true,
};

app.get("/code", async (req, res) => {
  const data =   await retrieveCode(request, optionsTOKEN);
  console.log(res.statusCode)
  res.send(data);
});

app.listen(8888, () => {
  console.log("server running on port 8888");
});

async function retrieveCode(URlRequest, options) {
  try {
    const res = await fetch(URlRequest, options);
    console.log(res);
    const data = await res.json();
    console.log("la data vaut" + data);
    return data
  } catch (err) {
    console.log(`L'erreur: ${err}`);
  }
}

`
谢谢你的帮忙
我尝试修改选项中的参数,在我的面板上设置一个新项目,更改端口。我希望检索访问令牌

oyt4ldly

oyt4ldly1#

您需要在“Basic”和代码之间添加一个空格
之前

"Basic" +code

之后

"Basic " +code

1使用此代码

此完整测试代码隐藏了client_id和client_secret

const express = require("express");
const fetch = (...args) =>
    import('node-fetch').then(({ default: fetch }) => fetch(...args));

const request = "https://accounts.spotify.com/api/token";
const client_id = "32-your-client-id-7b";
const client_secret = "ab-your-client_secret-9e";
const code = Buffer.from(client_id + ":" + client_secret).toString("base64");

const app = express();
const optionsTOKEN = {
    method: "POST",
    body: "grant_type=client_credentials",
    headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": "Basic " + code
    },
    // json: true,
};

app.get("/code", async (req, res) => {
    const data = await retrieveCode(request, optionsTOKEN);
    console.log(res.statusCode)
    res.send(data);
});

app.listen(8888, () => {
    console.log("server running on port 8888");
});

async function retrieveCode(URlRequest, options) {
    try {
        const res = await fetch(URlRequest, options);
        console.log(res);
        const data = await res.json();
        console.log("la data vaut" + JSON.stringify(data));
        return data
    } catch (err) {
        console.log(`L'erreur: ${err}`);
    }
}

2使用此依赖项

用于npm install的package.json

{
  "dependencies": {
    "express": "^4.18.2",
    "node-fetch": "^3.3.0"
  }
}

3 npm install

4 npm start

5从浏览器访问http://localhost:8888/code

控制台中的响应

la data vaut{"access_token":"BQCuVhXlpVQGKGxqK-remove-some-string-nX6sQp8uPSYBMh5lsU","token_type":"Bearer","expires_in":3600}
200

在浏览器中,

相关问题