node.js - {NodeJS} 如何从 HTTP 请求存储 Token API(发布)
问题描述
我在这里寻求帮助,我是 NodeJS 的新手,我想创建一个 Discord Bot。
我目前正在使用(npm) Discord.Js & Http; 我已经成功创建了一个 http post 请求来获取我的 api 令牌:
const infos = JSON.stringify({ grant_type: "password", username: "arius", password: "myanwsomepwd" });
const options = {
hostname: 'arius.freeboxos.fr',
port: 5500,
path: '/api/oauth/token',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': infos.length
}};
const req = http.request(options, res => {
res.on('data', d => {
process.stdout.write(d)
const MParse = JSON.parse(d)
const MyToken = MParse.access_token
})
});
req.on('error', error => {
console.error(error)
});
req.write(infos)
req.end()
这会正确返回我所有需要的信息,但我想保存/存储这个令牌(MyToken)以便在 GET 请求上使用它:
const options = {
hostname: 'arius.freeboxos.fr',
port: 5500,
path: '/api/v1/info',
method: 'GET',
headers: {
'Authorization': `Bearer ${MyToken}`
}};
const req = http.request(options, res => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', d => {
process.stdout.write(d)
})
})
req.on('error', error => {
console.error(error)
})
req.end()
我知道“'Authorization': Bearer ${MyToken}
”是错误的类型,它只是为了解释请求的目标。(我不想实际使用数据库或外部文件来保存此令牌)
我的 index.js :
const discord = require('discord.js')
const ping = require('./commands/ping')
const suppr = require('./commands/suppr')
// const authapi = require('./commands/authapi')
const apisinfos = require('./api/info')
const clients = new discord.Client()
clients.login('MyAnwsomeDiscordBotToken')
clients.once('ready', () => {
console.log('All right !')
})
clients.on('message', message => {
ping.parse(message) ||
suppr.parse(message) ||
// authapi.parse(message) ||
apisinfos.parse(message)
})
我的 api/cmds.js 类:
module.exports = class Cmds {
static parse (message) {
if (this.match(message)) {
this.action(message)
return true
}
return false
}
static match (message) {
return false
}
static action (message) {
}}
infos.js:
const Cmds = require('./cmds')
const http = require('http')
module.exports = class Apisinfo extends Cmds {
static match (message) {
return message.content.startsWith('!api-info')
}
static action (message) {
const infos = JSON.stringify({ grant_type: "password", username: "arius", password: "AnwsomePassword" });
const options = {
hostname: 'arius.freeboxos.fr',
port: 5500,
path: '/api/oauth/token',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': infos.length
}};
const req = http.request(options, res => {
res.on('data', d => {
process.stdout.write(d)
const MParse = JSON.parse(d)
const tok = MParse.access_token
})
});
req.on('error', error => {
console.error(error)
});
req.write(infos)
req.end()
}
}
感谢阅读<3!
解决方案
推荐阅读
- powershell - Powershell DSC 无法调用 Invoke-DscResource cmdlet
- javascript - 在 Angular 6 的组件中访问 *ngFor last
- django - 使用多对多相关字段计数对列表视图进行排序
- python - 当我们有多个变量时,ML 或时间序列模型?
- entity-framework-core - 如何使用 EF Core 插入 00000000-0000-0000-0000-000000000000?
- php - 表单中的必填字段
- angular - Angular 5 HttpResponse 状态 412 未捕获
- javascript - 来自反应客户端的请求量后,带有 express 的 socket.io 变慢
- java - 不同集合中的密码和用户名(Spring 安全性)
- image - 在燃气表上执行 OCR