tabula tabula - 19 days ago 7
Javascript Question

Get all public github repostories of user with javascript/node js

I want to get all public github repositories of given user from github.
I tried to make it with GET request I read from here. When i try it with curl or in the browser everything is fine, but when I run this code is gives me http 403 status code



var request = require('request');

request.get("https://api.github.com/users/:user")
.on('response', function (response) {
console.log(response.statusCode);
console.log(JSON.stringify(response));
});





I tried using this github api library, but couldn't work around the authetication



var GithubApi = require("github");

var github = new GithubApi({});


github.authenticate({
type: "oauth",
token: "GIT_TOKEN"
});

var getUsersRepos = function (user) {
github.repos.getAll({
username: user
}, function (err, res) {
res.forEach(function (element) {
console.log(`${element.name} - language: ${element.language} - issues: ${element.open_issues} - url: ${element.url}`);
}, this);
});
}

module.exports = {
getRepos: getUsersRepos
};





But when I enter my token I can get only my user information.
Any ideas what I am doing wrong or some better idea will be appreciated

Answer

Github seems to reject requests without a User-Agent header. This works:

var request = require('request');

options =  {
    url: "https://api.github.com/users/:user",
    headers: {
        "User-Agent": "foo"
    }
}

request.get(options)
    .on('response', function (response) {
        console.log(response.statusCode);
        console.log(JSON.stringify(response));
    });
Comments