收到401" Bad Credentials"当命中github API端点时

时间:2017-06-12 01:58:03

标签: node.js github request

我正在尝试构建一个使用GitHub api的CLI。我立即碰到了路障。虽然我上下阅读了介绍性文档,但我在下面的代码中看不出有什么问题。

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.
    });

  },
}

打印:

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

1 个答案:

答案 0 :(得分:2)

要获得自己的个人资料,您想要点击authenticated user endpoint,您不应该用自己的用户名替换它,GitHub会根据您的身份验证字符串知道您的身份:

https://api.github.com/user

要获取其他用户的个人资料,您需要点击users endpoint

https://api.github.com/users/:username