0

我正在尝试编写一个小 CLI 实用程序来自动生成 ~/.netrc 文件并将 github oauth 令牌存储在其中。

运行 curl 时,我可以从 github 获取新令牌。这有效

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

我想做完全相同的事情,但使用我的节点应用程序中的Superagent

不起作用。我只是得到一个403 响应

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

   });
}

我不应该能够模仿 curl 的作用吗?我错过了某些标题吗?有什么想法吗?

Github 文档供参考:
https ://help.github.com/articles/creating-an-oauth-token-for-command-line-use
http://developer.github.com/v3/oauth/#create-a -新授权

4

1 回答 1

1

原来 Github 要求传递“User-Agent”标头。默认情况下,Curl 会执行此操作。

这修复了它(显然我将更改用户代理,因为我目前正在模拟 Curl):

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')
...
于 2013-05-15T22:08:50.080 回答