2016-11-21 69 views
1

我有兩個curl字符串,它們先執行OAuth2-Token-Request,然後從API加載數據。現在我想將它包含到一個node.js插件中,所以我需要從插件中完成此操作。將我的curl POST變爲NodeJS OAuth2令牌請求

捲曲:

curl -X POST -d 'grant_type=password&client_id=8d3c1664-05ae-47e4-bcdb-477489590aa4&client_secret=4f771f6f-5c10-4104-bbc6-3333f5b11bf9&username=email&password=password' https://api.hello.is/v1/oauth2/token 

Test.js:

var request = require('request'); 

request({ 
    url: 'https://api.hello.is/v1/oauth2/token', 
    mehtod: "POST", 
    auth: { 
     username: 'email', 
     password: 'password' 
    }, 
    form: { 
     'grant_type': 'password', 
     'client_id': '8d3c1664-05ae-47e4-bcdb-477489590aa4', 
     'client_secret': '4f771f6f-5c10-4104-bbc6-3333f5b11bf9' 
    } 
}, function(err, res) { 
    var json = JSON.parse(res.body); 
    console.log("Access Token:", json.access_token) 
}); 

的問題是,我得到的回覆是唯一:{ code: 405, message: 'Method not allowed' }而捲曲給我的權利的access_token。

任何人都可以幫忙嗎?謝謝!!

+1

它看起來像有在你的代碼一個錯字:mehtod =>方法 –

+0

哈哈。這樣的菜鳥錯誤......現在它說「{code:401,消息:'未經授權。' }「 –

回答

1

也許嘗試:

var request = require('request'); 

request({ 
    url: 'https://api.hello.is/v1/oauth2/token', 
    mehtod: "POST", 
    form: { 
     username: 'email', 
     password: 'password', 
     grant_type: 'password', 
     client_id: '8d3c1664-05ae-47e4-bcdb-477489590aa4', 
     client_secret: '4f771f6f-5c10-4104-bbc6-3333f5b11bf9' 
    } 
}, function(err, res) { 
    var json = JSON.parse(res.body); 
    console.log("Access Token:", json.access_token) 
}); 
+0

坦克!這工作...是從網上教程快樂複製一小部分;) –