我試圖利用Google的授權服務guide。 我無法從服務器交易令牌中的代碼。而不是「方法:‘POST’」:響應:從節點服務器請求令牌時出現Invalid_request
var token_request='?code='+code+
'&client_id='+client_id+
'&client_secret='+client_secret+
'&redirect_uri='+redirect_uri+
'&grant_type=authorization_code';
options = {
host: "accounts.google.com",
path: '/o/oauth2/token'+token_request,
method: "POST"
}
var tokenRequest = https.request(options, function(res){
var resp = "";
res.on('data', function(data){
resp+= data;
})
res.on('end', function(){
console.log(resp);
})
res.on('error', function(err){
console.log("\033[;33mIt's an Error.\033[0;39m");
console.log(err);
})
}).end();
我認爲你是在正確的軌道上,而是倒退,它應該是POST,但不是像GET那樣將token_request添加到url中,而是需要將其添加到body中。有關語法,請參閱:http://nodejs.org/api/http.html#http_http_request_options_callback。雖然imho你應該使用請求模塊,因爲它是一個更好的api。 – generalhenry 2013-03-15 03:56:34
使用您當前的代碼,我認爲您只需將POST更改爲GET,我認爲應該這樣做。否則,您將不得不重新編寫代碼以執行POST請求。 – Jsterman 2013-03-15 15:13:13
@jsterman,我同意你的看法,但Google需要POST請求。說get不允許:[GET示例](https://accounts.google.com/o/oauth2/token)。 – BenJamin 2013-03-15 16:30:03