2013-03-14 87 views
0

我試圖利用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(); 

回答

1

我就從this網站,你應該使用「‘GET’方法」說,因爲你的價值觀是在查詢字符串。

編輯:

根據該意見,我會說,你必須返工,以便您的代碼,使其正常工作。

+1

我認爲你是在正確的軌道上,而是倒退,它應該是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

+1

使用您當前的代碼,我認爲您只需將POST更改爲GET,我認爲應該這樣做。否則,您將不得不重新編寫代碼以執行POST請求。 – Jsterman 2013-03-15 15:13:13

+0

@jsterman,我同意你的看法,但Google需要POST請求。說get不允許:[GET示例](https://accounts.google.com/o/oauth2/token)。 – BenJamin 2013-03-15 16:30:03

0

說實話我試圖做同樣的事情有困難。不過這不值得嘗試googleapis。

您需要使用NPM安裝谷歌的API

NPM安裝googleapis

看到https://npmjs.org/package/googleapis

的文檔