2013-05-15 32 views
0

我正在嘗試編寫一個小的CLI實用程序來自動生成〜/ .netrc文件並在其中存儲github oauth令牌。Superagent調用發出Github宣誓令牌在node.js中失敗應用

當我運行卷曲時,我能夠從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從我節點應用程序。

This does not work。我只是得到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); 

    }); 
} 

我不應該能夠模擬做什麼捲曲?我是否缺少某些標題?有什麼想法嗎?

Github的文檔以供參考:
https://help.github.com/articles/creating-an-oauth-token-for-command-line-use
http://developer.github.com/v3/oauth/#create-a-new-authorization

回答

1

原來Github上要求一個 「用戶代理」 首部傳遞。 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') 
...