Javascript 如何在需要API密钥的Google Apps脚本中使用外部API?
是否可以在谷歌应用程序脚本中使用需要API密钥的外部API 如何使用Google Apps脚本从需要密钥的API中获取数据?Apps脚本具有获取资源并通过Internet与其他主机通信的功能。这包括带有API键的URL请求 例如: 示例代码:Javascript 如何在需要API密钥的Google Apps脚本中使用外部API?,javascript,api,google-apps-script,xmlhttprequest,urlfetch,Javascript,Api,Google Apps Script,Xmlhttprequest,Urlfetch,是否可以在谷歌应用程序脚本中使用需要API密钥的外部API 如何使用Google Apps脚本从需要密钥的API中获取数据?Apps脚本具有获取资源并通过Internet与其他主机通信的功能。这包括带有API键的URL请求 例如: 示例代码: function myFunction() { var ss = SpreadsheetApp.getActiveSpreadsheet(); //get active spreadsheet var sheet = ss.getSheetByNa
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); //get active spreadsheet
var sheet = ss.getSheetByName('data'); //get sheet by name from active spreadsheet
var apiKey = 'Your API Key'; //apiKey for forecast.io weather api
var long = "-78.395602";
var lat = "37.3013648";
var url = 'https://api.forecast.io/forecast/' + apiKey +"/" + lat +"," + long; //api endpoint as a string
var response = UrlFetchApp.fetch(url); // get api endpoint
var json = response.getContentText(); // get the response content as text
var data = JSON.parse(json); //parse text into json
Logger.log(data); //log data to logger to check
var stats=[]; //create empty array to hold data points
var date = new Date(); //create new date for timestamp
//The following lines push the parsed json into empty stats array
stats.push(date);//timestamp
stats.push(data.currently.temperature); //temp
stats.push(data.currently.dewPoint); //dewPoint
stats.push(data.currently.visibility); //visibility
//append the stats array to the active sheet
sheet.appendRow(stats)
}
这个问题太宽泛了。不要在源代码中以纯文本形式存储API密钥。