首页 > 解决方案 > {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!

标签: node.jsapidiscorddiscord.js

解决方案


推荐阅读