【发布时间】:2018-04-17 23:56:14
【问题描述】:
对 spotify API 的 curl 请求运行良好
curl -X "POST" -H "Authorization: Basic <my-key-here>" -d grant_type=client_credentials https://accounts.spotify.com/api/token
我正在尝试在节点中执行此操作,但它不起作用并返回 400 错误请求。这是我的代码。我做错了什么?
function AuthRequest(key) {
const req_body_params = JSON.stringify({
grant_type: "client_credentials"
})
const base64_enc = new Buffer(key).toString("base64")
const options = {
host: "accounts.spotify.com",
port: 443,
path: "api/token",
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Basic ${base64_enc}`
}
}
const req = https.request(options, function (res) {
res.on('data', function (data) {
alert("success: " + data)
})
})
req.on('error', function (err) {
alert("error: " + err)
})
req.write(req_body_params)
req.end()
}
我正在尝试使用此处解释的客户端凭据方法:https://developer.spotify.com/web-api/authorization-guide/
【问题讨论】:
-
您遇到的错误是什么?
-
400 错误请求
-
您缺少必要的参数,例如代码和 redirect_uri
-
您的 req 正文参数应该是您的选项对象的一部分,它们应该在查询参数语法中指定。将
body: 'grant_type=client_credentials'添加到您的选项对象并重试请求并注释掉req.write行 -
没有帮助。一样的回应。还尝试了
application/x-www-form-urlencoded,就像@AngYC 建议的那样。没用。
标签: node.js curl https request