授权流程 returns 400
Authorization Flow returns 400
我已经尝试解决这个问题好一个小时了,但我似乎无法解决这个问题。我正在为我的请求使用 node-fetch
(这只是浏览器的 Fetch API 的副本,但为 Node 克隆)。我有以下代码:
fetch("https://accounts.spotify.com/api/token", {
body: `grant_type=authorization_code&code=${req.query.code}&redirect_uri=${encodeURIComponent("http://localhost:3000/callback")}`,
headers: {
Authorization: `Basic ${btoa(my_client_id)}:${btoa(my_client_secret)}`,
"Content-Type": "application/x-www-form-urlencoded"
},
method: "POST"
})
这应该从回调中获取代码,并将其发送到 Spotify 以获得刷新令牌。
预期行为:
返回的应该是以下内容:
实际行为:
400 错误请求(一致)
这行得通。看起来您对凭证的编码不正确。
它 base64({id}:{secret})
不是 base64({id}):base64({secret})
.
我使用 request-promise
纯粹是为了方便,所以如果您在 fetch 中修复 Authorization
header,它应该可以工作。
const rp = require('request-promise');
const clientId = 'xxx';
const clientSecret = 'xxx';
async function auth() {
const credentialsBuffer = Buffer.from(`${clientId}:${clientSecret}`);
const options = {
method: 'POST',
uri: 'https://accounts.spotify.com/api/token',
form: {
grant_type: 'client_credentials'
},
headers: {
Authorization: `Basic ${credentialsBuffer.toString('base64')}`
},
};
const resp = await rp(options);
console.log(resp);
}
(async () => {
await auth();
})()
我已经尝试解决这个问题好一个小时了,但我似乎无法解决这个问题。我正在为我的请求使用 node-fetch
(这只是浏览器的 Fetch API 的副本,但为 Node 克隆)。我有以下代码:
fetch("https://accounts.spotify.com/api/token", {
body: `grant_type=authorization_code&code=${req.query.code}&redirect_uri=${encodeURIComponent("http://localhost:3000/callback")}`,
headers: {
Authorization: `Basic ${btoa(my_client_id)}:${btoa(my_client_secret)}`,
"Content-Type": "application/x-www-form-urlencoded"
},
method: "POST"
})
这应该从回调中获取代码,并将其发送到 Spotify 以获得刷新令牌。
预期行为:返回的应该是以下内容:
400 错误请求(一致)
这行得通。看起来您对凭证的编码不正确。
它 base64({id}:{secret})
不是 base64({id}):base64({secret})
.
我使用 request-promise
纯粹是为了方便,所以如果您在 fetch 中修复 Authorization
header,它应该可以工作。
const rp = require('request-promise');
const clientId = 'xxx';
const clientSecret = 'xxx';
async function auth() {
const credentialsBuffer = Buffer.from(`${clientId}:${clientSecret}`);
const options = {
method: 'POST',
uri: 'https://accounts.spotify.com/api/token',
form: {
grant_type: 'client_credentials'
},
headers: {
Authorization: `Basic ${credentialsBuffer.toString('base64')}`
},
};
const resp = await rp(options);
console.log(resp);
}
(async () => {
await auth();
})()