Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"}
like image 648
Alex Bollbach Avatar asked May 29 '26 03:05

Alex Bollbach


1 Answers

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
like image 90
sjdaws Avatar answered May 31 '26 18:05

sjdaws



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!