在google apps script如何使用api取資料?
2017/11/02 10:26 by secde
之前在google app script上取用只要用網址就好了
如:UrlFetchApp.fetch("http://ptx.transportdata.tw/MOTC/v2/Rail/TRA/Station?$format=JSON")
就能取得json的資料了
但現在不行了…
我照著:https://gist.github.com/ptxmotc/383118204ecf7192bdf96bc0197bb981
也沒成功,目前一直出現403
目前我的寫的是…
var url="http://ptx.transportdata.tw/MOTC/v2/Rail/TRA/Station?$format=JSON";
var xdate=new Date().toGMTString();
var l2_id="xxxxxxxxxxxxxxxxxxxxxxxxx";
var l2_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
//HMAC-SHA1 運算
var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_1,xdate,l2_key);
//轉成Base64
var signatured=Utilities.base64Encode(signature);
var header = {
'Authorization': 'hmac username='+l2_id+','+'algorithm=hmac-sha1,headers=x-date,signature='+signatured
}
var options = {
'headers': header,
'method': 'get'
}
var getans=UrlFetchApp.fetch(url,options)
但試很久都是失敗,請問要如何才能取得資料呢?