从Github获取访问令牌 [英] Getting access token from Github

查看:1061
本文介绍了从Github获取访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NodeJS客户端从Github获取访问令牌。

I'm trying to get an access token from Github using a NodeJS client.

const axios = require("axios");
var jwt = require("jsonwebtoken");

exports.openedPOST = function openedPOST(req, res) {

// generate jwt
const now = Math.round(Date.now() / 1000);
const payload = {
    // issued at time
    iat: now,
    // expires in 10min
    exp: now + 600,
    // Github app id
    iss: 6700
};

const token = jwt.sign(payload, cert, { algorithm: "RS256" });
console.log(token)

// auth to github
axios({
  method: "get",
  url: "https://api.github.com/app",
  headers: {
    Accept: "application/vnd.github.machine-man-preview+json",
    Authorization: `Bearer ${token}`
  }
})
.then(function(response) {
  console.log(response.data);
})
.catch(function(error) {
  console.warn("Unable to authenticate");
  // The request was made and the server responded with a status code
  // that falls out of the range of 2xx
  if (error.response) {
    console.warn(`Status ${error.response.status}`);
    console.warn(`${error.response.data.message}`);
  }
});

res.status(200).end();

但这只产生:

{
message:无法解码JSON网络令牌,
documentation_url:https://developer.github.com/v3
}

我已在 https://验证了令牌jwt.io 和有效负载是预期的。

I have verified the token at https://jwt.io and the payload is as expected.

推荐答案

我得到了这个工作。它主要基于你所拥有的,但有一些调整:

I got this working. It's largely based on what you have but there are a few tweaks:

const axios = require("axios");
var fs = require('fs');
var jwt = require("jsonwebtoken");


exports.openedPOST = function openedPOST(req, res) {

  // Private key contents
  var private_key = fs.readFileSync("/path/to/pemfile.pem");
  console.log("private_key: ", private_key);

  // generate jwt
  const now = Math.round(Date.now() / 1000);
  const payload = {
    // issued at time
    iat : now,
    // expires in 10min
    exp : now + (10 * 60),
    // Github app id
    iss : 7233
  };
  console.log("payload: ", payload);

  const token = jwt.sign(payload, private_key, { algorithm: 'RS256' })
  console.log("Token: ", token)

  // auth to github
  var instance = axios({
    method: "get",
    url: "https://api.github.com/app",
    headers: {
      "Accept" : "application/vnd.github.machine-man-preview+json",
      "Authorization" : `Bearer ${token}`
    }
  })
  .then(function(response) {
    console.log("Response: ",response.data);
  })
  .catch(function(error) {
    console.warn("Unable to authenticate");
    // The request was made and the server responded with a status code
    // that falls out of the range of 2xx
    if (error.response) {
      console.warn(`Status ${error.response.status}`);
      console.warn(`${error.response.data.message}`);
    }
  });
};
exports.openedPOST();

我的主要问题是生成了private_key变量。 Alos,我将 600 更改为(10 * 60)因为我在调查的一个阶段遇到了不同的错误但结果证明不是问题。你在那里拥有什么并不重要,所以我离开了它。

The main issue for me was with the private_key variable got generated. Alos, I changed 600 to (10 * 60) as I had got a different error at one stage of my investigation but that turned out to not be the problem. It doesn't really matter what you have there so I left it.

我做的另一个改变是分配 axios 变量。我对node.js比较陌生,所以不确定为什么要这样做但是怀疑它与node.js的同步/异步方面有关。

The other change I made was to assign axios to a variable. I'm relatively new to node.js so not really sure why this had to be done but suspect its something to do with the synchronous/asynchronous aspect of node.js.

这篇关于从Github获取访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆