Alex Bollbach Alex Bollbach -4 years ago 114
Node.js Question

Receiving 401 "Bad Credentials" when hitting github API endpoint

I'm trying to build a CLI that uses the GitHub api. I instantly run into a road block. Although I've read the introductory docs up and down, I don't see what is wrong in the following code.

var userData = require('../userData');
var request = require('request');

module.exports = {
hitEndpoint: function() {
var username = "<redacted_user_name>",
password = "<redacted_password>",
auth = "Basic " + new Buffer(username + ":" + password).toString("base64");

var options = {
method: 'get',
url: " https://api.github.com/<redacted_user_name>",
headers: {
"Authorization": auth,
"User-Agent": "<redacted_whatever_doesnt_matter>"
}
}
request(options, function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
});

},
}


prints:

error: null
statusCode: 404
body: {"message":"Not Found","documentation_url":"https://developer.github.com/v3"}

Answer Source

To get your own profile you'll want to hit the authenticated user endpoint, you shouldn't replace this with your own username, GitHub will know who you are based on your authentication string:

https://api.github.com/user

To get another user's profile you'll want to hit the users endpoint:

https://api.github.com/users/:username
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download