简体   繁体   English

击中github API端点时收到401“不良凭证”

[英]Receiving 401 “Bad Credentials” when hitting github API endpoint

I'm trying to build a CLI that uses the GitHub api. 我正在尝试构建一个使用GitHub API的CLI。 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"}

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: 要获取自己的个人资料,您需要点击已验证的用户端点 ,您不应将其替换为您自己的用户名,GitHub会根据您的身份验证字符串知道您是谁:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM