Sagar Chaudhary
Sagar Chaudhary

Reputation: 1403

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

I am trying to get data of below url using sync-request module.

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

I get the data when i call it in browser or through postman.
But i am getting 403 forbidden error when calling it using sync-request in my node api.

My code looks like this.

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

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

I am able to fetch data of many other api's but not this one. Please help.

Upvotes: 3

Views: 259

Answers (2)

Janith Udara
Janith Udara

Reputation: 685

Try to use an access token along with the GitHub API call like this

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

As you say the API works in the browser it should not be an issue.

When you use too many calls through the GitHub API they they give the following message

{ "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" }

To overcome this issue you can use an access token using an access token you can to access the private repositories in your account as well . Here is the link for to get an access token [https://github.com/settings/developers]

Upvotes: 1

Estus Flask
Estus Flask

Reputation: 222603

Response body already contains the explanation:

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.

It will work like:

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

The use of sync-request is strongly discouraged because synchronousness is achieved via a hack and may block the process for a long time.

For sequential execution request-promise can be used together with async..await.

Upvotes: 1

Related Questions