使用Request的request函数时,Github API响应为403 [英] Github API is responding with a 403 when using Request's request function

查看:150
本文介绍了使用Request的request函数时,Github API响应为403的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向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 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.

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的request函数时,Github API响应为403的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆