使用 Request 的请求功能时,Github API 响应 403 [英] Github API is responding with a 403 when using Request's request function
问题描述
我正在尝试向 github 的 API 发出请求.这是我的要求:
I am trying to make a request to github's API. Here is my request:
var url = 'https://api.github.com/' + requestUrl + '/' + repo + '/';
request(url, function(err, res, body) {
if (!err && res.statusCode == 200) {
var link = "https://github.com/" + repo;
opener(link);
process.exit();
} else {
console.log(res.body);
console.log(err);
console.log('This ' + person + ' does not exist');
process.exit();
}
});
这是我得到的回复:
Request forbidden by administrative rules. Please make sure your request has a User-Agent header (http://developer.github.com/v3/#user-agent-required). Check https://developer.github.com for other possible causes.
我过去曾使用过完全相同的代码,并且奏效了.请求不会引发任何错误.现在我对为什么我收到 403(禁止)感到困惑?有什么解决办法吗?
I have used the exact same code in the past, and it has worked. No errors are being thrown by request. Now I am confused with why I am getting a 403 (Forbidden)? Any solutions?
推荐答案
如 响应中给出的 URL,对 GitHub API 的请求现在需要 User-Agent
标头:
As explained in the URL given in the response, requests to GitHub's API now require a User-Agent
header:
所有 API 请求都必须包含有效的 User-Agent
标头.没有 User-Agent
标头的请求将被拒绝.我们要求您使用 GitHub 用户名或应用程序名称作为 User-Agent
标头值.这样我们就可以在出现问题时与您联系.
All API requests MUST include a valid
User-Agent
header. Requests with noUser-Agent
header will be rejected. We request that you use your GitHub username, or the name of your application, for theUser-Agent
header value. This allows us to contact you if there are problems.
request
文档具体展示如何在您的请求中添加 User-Agent
标头:
The request
documentation shows specifically how to add a User-Agent
header to your request:
var request = require('request');
var options = {
url: 'https://api.github.com/repos/request/request',
headers: {
'User-Agent': 'request'
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
console.log(info.stargazers_count + " Stars");
console.log(info.forks_count + " Forks");
}
}
request(options, callback);
这篇关于使用 Request 的请求功能时,Github API 响应 403的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!