gpt4 book ai didi

node.js - 使用 Request 的请求函数时,Github API 以 403 响应

转载 作者:搜寻专家 更新时间:2023-10-31 22:27:38 24 4
gpt4 key购买 nike

我正在尝试向 github 的 API 发出请求。这是我的要求:

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(禁止访问)?有什么解决办法吗?

最佳答案

URL given in the response 中所述,对 GitHub 的 API 的请求现在需要一个 User-Agent header :

All API requests MUST include a valid User-Agent header. Requests with no User-Agent header will be rejected. We request that you use your GitHub username, or the name of your application, for the User-Agent header value. This allows us to contact you if there are problems.

请求文档shows specifically如何将 User-Agent header 添加到您的请求中:

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

关于node.js - 使用 Request 的请求函数时,Github API 以 403 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39907742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com