2017-06-13 75 views
0

我嘗試使用Google應用程序腳本在我的DirectEnergie帳戶中驗證自己。 當我把這個請求放在POSTMAN和Chrome上時,它會起作用,但是當我嘗試跟隨時,它會返回第一頁的HTML,但沒有進行驗證。Google script HTMLRequest - Post

function getSumParr(){ 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = ss.getSheets()[0]; 
    url = "https://particuliers.direct-energie.com/mon-espace-client/?tx_deauthentification[login]=login&tx_deauthentification[password]=motdepasse&tx_deauthentification[form_valid]=1&tx_deauthentification[redirect_url]= " 
    var resultat = UrlFetchApp.fetch(url); 
    Logger.log(resultat); 
} 

當我遵循郵遞員請求的歷史,我有這樣的: 響應[303]回覆[307]

任何人對如何計算出來的想法?

+0

堆棧片段用於能夠直接在問題/答案上運行的代碼,換言之,即客戶端代碼。由於問題是關於服務器端代碼,我刪除了堆棧片段。 –

+0

謝謝ruben,我學到了東西 –

+0

不客氣。關於你問題中的URL,可能問題可能與括號的使用有關。你的網址需要括號嗎?如果是這樣,你應該編碼你的URL。 –

回答

1

您可能要檢查本教程的GET and POST Requests in Google Apps Script

與谷歌Apps腳本,您可以輕鬆地創建的Web App提供HTML,JSON,XML或使用HTML服務純文本輸出。當作爲應用程序發佈時,該腳本將獲取一個公用URL,可以使用帶有參數的GET或POST請求來調用該URL。

這裏是他們的代碼片段:

function testPOST() { 

    var url = ScriptApp.getService().getUrl(); 

    var payload = 
     { 
     "name" : "labnol", 
     "blog" : "ctrlq", 
     "type" : "post", 
     }; 

    var options = 
     { 
     "method" : "POST", 
     "payload" : payload, 
     "followRedirects" : true, 
     "muteHttpExceptions": true 
     }; 

    var result = UrlFetchApp.fetch(url, options); 

    if (result.getResponseCode() == 200) { 

    var params = JSON.parse(result.getContentText()); 

    Logger.log(params.name); 
    Logger.log(params.blog); 

    } 

} 

此示例也遵循關於如何使使用可選的高級參數獲取一個URL的請求documentation

希望這會有所幫助。

+0

謝謝Rebot先生。我已經看過這個頁面,並嘗試過但仍然是相同的結果。 不明白爲什麼在郵遞員它的作品,爲什麼在Ggscript不。任何其他想法? –

相關問題