【问题标题】:Github API is responding with a 403 when using Request's request function使用 Request 的请求功能时,Github API 以 403 响应
【发布时间】:2022-04-19 00:55:56
【问题描述】:

我正在尝试向 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(禁止)?有什么解决办法吗?

【问题讨论】:

  • 添加用户代理标头或仅将 npm 上的模块用于 github API。

标签: node.js request github-api


【解决方案1】:

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

所有 API 请求都必须包含有效的 User-Agent 标头。没有User-Agent 标头的请求将被拒绝。我们要求您使用您的 GitHub 用户名或应用程序的名称作为 User-Agent 标头值。这样我们可以在有问题时与您联系。

request 文档 shows specifically 如何在您的请求中添加 User-Agent 标头:

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

【讨论】:

    【解决方案2】:

    从 C# 并使用 HttpClient 你可以做到这一点(在最新的 .Net Core 2.1 上测试):

    using (HttpClient client = new HttpClient())
    {
        client.DefaultRequestHeaders.Accept.Add( new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    
        client.DefaultRequestHeaders.UserAgent.TryParseAdd("request");//Set the User Agent to "request"
    
        using (HttpResponseMessage response = client.GetAsync(endPoint).Result)
        {
            response.EnsureSuccessStatusCode();
            responseBody = await response.Content.ReadAsByteArrayAsync();
        }
    }
    

    谢谢

    【讨论】:

      【解决方案3】:

      如果 Github API 响应状态码:403,这只是意味着 您的 API 获取限制已超出,请等待 1 分钟,然后再次获取,然后您将获取您的数据,

      【讨论】:

        【解决方案4】:

        角度

        我遇到了同样的问题,即超出了 403 速率限制。我尝试使用 angular/common 中的 new HttpHeaders 设置 Authorization 标头,但标头仍然失败。 失败的代码是:

        getUser(username: string): Observable<any> {
            this.headers.set('Authorization', `token ${this.ACCESS_TOKEN}`);
            const endpoint = `https://api.github.com/users/${username}`;
            return this.http.get(endpoint, {
              headers: this.headers
            }).pipe(response => {
              return response
            });
          }
        

        有效的代码:

        getUser(username: string): Observable<any> {
            const endpoint = `https://api.github.com/users/${username}`;
            return this.http.get(endpoint, {
              headers: {
                Authorization: `token ${this.ACCESS_TOKEN}`,
              }
            }).pipe(response => {
              return response
            });
          }
        

        这种情况下,我直接在request headers段中设置header。

        【讨论】:

          猜你喜欢
          • 2021-11-17
          • 2022-01-17
          • 2019-01-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-11
          • 2016-08-17
          相关资源
          最近更新 更多