tabula
tabula

Reputation: 243

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

Upvotes: 4

Views: 2459

Answers (1)

Leftium
Leftium

Reputation: 17903

The Github API requires requests to have a valid User-Agent header:

If you provide an invalid User-Agent header, you will receive a 403 Forbidden response.

Github requests that you use your GitHub username, or the name of your application, for the User-Agent header value:

var request = require('request');

options =  {
    url: "https://api.github.com/users/:user",
    headers: {
        "User-Agent": "tabula"  // Your Github ID or application name
    }
}

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

Upvotes: 4

Related Questions