我正在尝试编写一个小的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-new-authorization
答案 0 :(得分: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')
...