Question

I am trying to write a little CLI utility to auto-generate a ~/.netrc file and store the github oauth token inside.

I am able to get a new token from github when I run curl. This works:

curl -u 'my_username' -d '{"scopes":["repo"],"note":"Help example"}' \
https://api.github.com/authorizations

https://help.github.com/articles/creating-an-oauth-token-for-command-line-use

I want to do the exact same thing, but with Superagent from my node app.

This does not work. I simply get a 403 response:

function getGitToken(user, pwd) {
   console.log(user + "|" + pwd)

   var url = "https://api.github.com/authorizations";

   request
    .post(url)
    .auth(user, pwd)
    //.send({"username": "#####"})
    .send({"scopes":["repo"], "note":"Easy-netrc generated"}) //TODO: add support for shell prompt name
    .end(function(res){
       console.log(res);
       console.log("res.body", res.body);

   });
}

Shouldn't I be able to emulate what curl does? Am I missing certain headers? Any thoughts?

Github docs for reference:
https://help.github.com/articles/creating-an-oauth-token-for-command-line-use
http://developer.github.com/v3/oauth/#create-a-new-authorization

Was it helpful?

Solution

Turns out Github requires that a "User-Agent" header be passed. Curl does this by default.

This fixed it (obviously I'll be changing the user-agent, since I'm emulating Curl currently):

request
...
.set('User-Agent', 'curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5')
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top