响应 <403> 尽管 nodejs 中的正确授权
Response <403> Despite Correct Authorization in nodejs
我正在尝试向 URL 发送 post 请求,我在 python 中使用以下代码完成了此操作,它非常有效,我得到了 [Response <200>],但由于我需要在网站中使用它,所以我切换到 JS 并尝试重新创建相同的功能,但出于某种原因,我得到了 [Response <403>] 甚至我所有的身份验证令牌和 headers 一切都与 python 代码相同。
Python代码-
url = "https://discord.com/api/v8/channels/801784356711956522/messages"
auth = ""
headers = {"Authorization": auth,
'Content-Type': 'application/json', 'referer': "https://discord.com/channels/801784356217421874/801784356711956522"}
payload = {'content': 'Test' , 'nounce': 802056256326991872, 'tts': False}
response = requests.post(url, data=json.dumps(payload), headers=headers)
print(response)
JavaScript代码-
onst url = "https://discord.com/api/v8/channels/801784356711956522/messages"
const auth = ""
const headers = {"Authorization": auth,
'Content-Type': 'application/json',
'referer': "https://discord.com/channels/801784356217421874/801784356711956522"}
const options = {
headers : headers,
}
const data = JSON.stringify({'content':"Test" , 'nounce': 802056256326991872, 'tts': false})
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
const req = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', (d) => {
process.stdout.write(d)
})
})
req.on('error', (error) => {
console.error(error)
})
req.write(data)
req.end()
在您的 python 代码中,您发出了 POST 请求,但在 JavaScript 代码中,您发出了 GET 请求,因为你没有提供 method 选项。
它在 https.request 选项文档中指定:
method A string specifying the HTTP request method. Default:
'GET'.
使POST请求像这样修改
const options = {
headers : headers,
method: "POST"
}
此外,您需要添加 URL,因为您没有在选项中提供主机名和路径。
const req = https.request(url, options, (res) => {
// ...
})
const querystring = require('querystring');
const https = require('https');
var postData = querystring.stringify({
'msg' : 'Hello World!'
});
var options = {
hostname: 'domain.com',
port: 443,
path: '/yow-path',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};
var req = https.request(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
process.stdout.write(d);
});
});
req.on('error', (e) => {
console.error(e);
});
req.write(postData);
req.end();
我正在尝试向 URL 发送 post 请求,我在 python 中使用以下代码完成了此操作,它非常有效,我得到了 [Response <200>],但由于我需要在网站中使用它,所以我切换到 JS 并尝试重新创建相同的功能,但出于某种原因,我得到了 [Response <403>] 甚至我所有的身份验证令牌和 headers 一切都与 python 代码相同。
Python代码-
url = "https://discord.com/api/v8/channels/801784356711956522/messages"
auth = ""
headers = {"Authorization": auth,
'Content-Type': 'application/json', 'referer': "https://discord.com/channels/801784356217421874/801784356711956522"}
payload = {'content': 'Test' , 'nounce': 802056256326991872, 'tts': False}
response = requests.post(url, data=json.dumps(payload), headers=headers)
print(response)
JavaScript代码-
onst url = "https://discord.com/api/v8/channels/801784356711956522/messages"
const auth = ""
const headers = {"Authorization": auth,
'Content-Type': 'application/json',
'referer': "https://discord.com/channels/801784356217421874/801784356711956522"}
const options = {
headers : headers,
}
const data = JSON.stringify({'content':"Test" , 'nounce': 802056256326991872, 'tts': false})
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
const req = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', (d) => {
process.stdout.write(d)
})
})
req.on('error', (error) => {
console.error(error)
})
req.write(data)
req.end()
在您的 python 代码中,您发出了 POST 请求,但在 JavaScript 代码中,您发出了 GET 请求,因为你没有提供 method 选项。
它在 https.request 选项文档中指定:
method A string specifying the HTTP request method. Default: 'GET'.
使POST请求像这样修改
const options = {
headers : headers,
method: "POST"
}
此外,您需要添加 URL,因为您没有在选项中提供主机名和路径。
const req = https.request(url, options, (res) => {
// ...
})
const querystring = require('querystring');
const https = require('https');
var postData = querystring.stringify({
'msg' : 'Hello World!'
});
var options = {
hostname: 'domain.com',
port: 443,
path: '/yow-path',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};
var req = https.request(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
process.stdout.write(d);
});
});
req.on('error', (e) => {
console.error(e);
});
req.write(postData);
req.end();