Node.js 节点JS Octokit身份验证和存储Github令牌

Node.js 节点JS Octokit身份验证和存储Github令牌,node.js,github,cmd,config,octokit,Node.js,Github,Cmd,Config,Octokit,目标 我正试图按照Sitepoint教程中概述的说明使用Node JS创建我的第一个CLI(请参阅) 对于那些试图问我为什么要在Node中创建它的人,这是我自己的一个练习,我不想通过使用其他方法来优化我的工作,但感谢您的考虑 问题 我正在尝试建立到Github的连接,允许组织的用户输入他们的凭据(用户名、密码和可能的2FA代码)来连接到组织帐户,以便访问存储库 目前,我可以利用Octokit将凭证传递给github,以接收和存储其令牌 错误: 未处理的PromisejectionWarning:

目标

我正试图按照Sitepoint教程中概述的说明使用Node JS创建我的第一个CLI(请参阅)

对于那些试图问我为什么要在Node中创建它的人,这是我自己的一个练习,我不想通过使用其他方法来优化我的工作,但感谢您的考虑

问题

我正在尝试建立到Github的连接,允许组织的用户输入他们的凭据(用户名、密码和可能的2FA代码)来连接到组织帐户,以便访问存储库

目前,我可以利用Octokit将凭证传递给github,以接收和存储其令牌

错误: 未处理的PromisejectionWarning:HttpError:用于双因素身份验证的一次性密码无效

代码:

module.exports = {
  getPath: () => {
    return conf.path;
  },
  getInstance: () => {
    return octokit;
  },
  getStoredGithubToken: () => {
    return conf.get('github.token');
  },
  setGithubCredentials: async () => {
    const credentials = await inquirer.askCredentials();
    _.extend(credentials, {
      async on2fa () {
        const questions = {
          name: 'authorization code',
          type: 'input',
          message: 'Two-factor authentication Code:',
          validate: function (value) {
            if (value.length) {
              return true;
            } else {
              return 'Two-factor authentication Code:';
            }
          }
        }
        return questions;
      }
    });
    module.exports.registerNewToken(credentials);
  },
  registerNewToken: async (credentials) => {
    const status = new Spinner('Authenticating you, please wait...');
    status.start();
    credentials.on2fa();
    try {
      const response = await octokit({
        auth: credentials
      });
      response.repos.listForOrg({
        org: '<organization>',
        type: '<type>'
      }).then(({data,status,headers})=>{
        console.log({data,status,headers});
      })
      const token = response;
      if (token) {
        conf.set('github.token', token);
      } else {
        throw new Error("Missing Token", "GitHub token was not found in the response");
      }
    } catch (err) {
      throw err;
    } finally {
      status.stop();
    }
  }
}
module.exports={
getPath:()=>{
返回conf.path;
},
getInstance:()=>{
返回八达通套件;
},
getStoredGithubToken:()=>{
返回conf.get('github.token');
},
setGithubCredentials:async()=>{
const credentials=wait inquirer.askCredentials();
_.扩展(证书、{
异步on2fa(){
常量问题={
名称:'授权码',
键入:“输入”,
消息:“双因素身份验证代码:”,
验证:函数(值){
if(值.长度){
返回true;
}否则{
返回“双因素身份验证代码:”;
}
}
}
回答问题;
}
});
模块.exports.registerNewToken(凭证);
},
registerNewToken:async(凭据)=>{
const status=new Spinner('正在验证您,请稍候…');
status.start();
on2fa();
试一试{
const response=等待({
认证:证书
});
response.repos.listForOrg({
组织:'',
类型:“”
})。然后({数据、状态、标题})=>{
log({data,status,headers});
})
const-token=响应;
如果(令牌){
conf.set('github.token',token);
}否则{
抛出新错误(“缺少令牌”,“响应中未找到GitHub令牌”);
}
}捕捉(错误){
犯错误;
}最后{
status.stop();
}
}
}