2017-12-25 11 views
0

Ich versuche, ein Zugriffstoken von Github mit einem NodeJS-Client zu erhalten.Getting-Access-Token von Github

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(); 

Aber das erzeugt nur: { "message": "A JSON web token could not be decoded", "documentation_url": "https://developer.github.com/v3" }

ich das Token bei https://jwt.io prüft haben, und die Nutzlast ist wie erwartet.

+0

hast du das jemals gelöst? – pac

+0

Nein. Keine Ahnung, warum es scheitert. – timbo

+0

Es mag keinen Unterschied machen, aber ich habe das manuell gemacht, indem ich ein Ruby-Skript benutzt habe, um das Token zu generieren. Ich habe festgestellt, dass "iss: 6700" zu der Nachricht führen würde, die Sie erhalten haben, aber "" iss: 6700 "" hat die Tokengenerierung erlaubt zu arbeiten. Entschuldigung, wenn ich aus dem Thema bekomme, aber wie haben Sie 'cert' generiert? – pac

Antwort

1

Ich habe das funktioniert. Es ist weitgehend auf das, was Sie haben, aber es gibt ein paar 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(); 

Die wichtigste Frage für mich mit der private_key Variable war, wurde erzeugt. Alos, ich änderte 600 zu (10 * 60), da ich einen anderen Fehler in einem Stadium meiner Untersuchung hatte, aber das stellte sich heraus, nicht das Problem zu sein. Es ist nicht wirklich wichtig, was du da hast, also habe ich es verlassen.

Die andere Änderung, die ich machte, war axios zu einer Variablen zuzuweisen. Ich bin relativ neu in node.js, also nicht wirklich sicher, warum dies getan werden musste, aber ich vermute, dass es etwas mit dem synchronen/asynchronen Aspekt von node.js zu tun hat.