未使用 npm 模块同步请求获取 GITHUB api 的数据

Not getting data of GITHUB api using npm module sync-request

我正在尝试使用同步请求模块获取以下 url 的数据。

https://api.github.com/repos/rethinkdb/rethinkdb/stargazers

当我在浏览器或通过邮递员调用它时,我得到了数据。
但是在我的节点 api.

中使用同步请求调用它时出现 403 禁止错误

我的代码如下所示。

 var request = require("sync-request");

 var response = request('GET', 'https://api.github.com/repos/rethinkdb/rethinkdb/stargazers', {
                headers: {},
                json: true
            });

我能够获取许多其他 api 的数据,但不能获取这一个。请帮忙。

响应正文已包含说明:

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.

它将像这样工作:

var response = request('GET', 'https://api.github.com/repos/rethinkdb/rethinkdb/stargazers', {
    headers: { 'User-Agent': 'Request' },
    json: true
});

强烈建议不要使用 sync-request,因为同步是通过 hack 实现的,可能会长时间阻塞进程。

对于顺序执行request-promise可以和async..await一起使用。

尝试在 GitHub API 调用中使用访问令牌 像这样

[https://api.github.com/repos/rethinkdb/rethinkdb/stargazers?access_token=f33d1f112b7883456c990028539a22143243aea9]

正如您所说 API 在浏览器中工作,这应该不是问题。

当您通过 GitHub API 使用太多电话时,他们会给出以下消息

{ "message": "API rate limit exceeded for 192.248.24.50. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)", "documentation_url": "https://developer.github.com/v3/#rate-limiting" }

要解决此问题,您可以使用访问令牌使用访问令牌,您也可以访问您帐户中的私有存储库。 这是获取访问令牌 [https://github.com/settings/developers]

的 link