繁体   English   中英

在脚本编辑器中设置Maps API密钥

[英]Set Maps API key in Script Editor

据我了解,为了跟踪我们的配额使用情况,我们需要在我们计划使用的服务上向Google App Service提供API密钥。

在我的例子中,我有一个带有Origin和Destination的电子表格以及一个自定义函数来计算它们之间的距离。

我遇到了通过调用.getDirections()来满足配额的问题:

错误:服务调用太多次一天:路由。 (行**)。

代码示例:

 function getDirections_(origin, destination) {
  var directionFinder = Maps.newDirectionFinder();
  directionFinder.setOrigin(origin);
  directionFinder.setDestination(destination);
  var directions = directionFinder.getDirections();  
  return directions;
}

所以我读到如果我将API密钥分配给我的项目,我应该能够看到使用情况以及我与免费配额的接近程度。

在脚本编辑器中,我确实启用了“资源”菜单/“高级Google服务”下的所有API。 然后我去了Google Developers Console,在那里我没有看到任何关于我的自定义函数调用Google Maps API或任何API使用次数的记录。

从逻辑上讲,我认为在我的脚本中我需要设置我的谷歌API密钥,以便我的脚本开始以我的用户名调用API并计算我使用某些API的时间。 我想我现在正在使用谷歌地图API作为匿名,因为整个公司被分配了相同的IP,所以我们用尽许多数字来调用这个功能。

如果你知道一种方法将我的简单电子表格功能连接到我拥有的公共API访问密钥,请回复底线。

谢谢你,保罗

我也渴望在很长一段时间内找到这个答案,我很高兴地说我找到了它。 看起来Google可能会在2015年10月14日左右根据此页面更新日期提供此功能

您可以利用UrlFetchApp添加API密钥。 我上面发布的链接应该有助于获取该密钥。

function directionsAPI(origin, destination) {
 var Your_API_KEY = "Put Your API Key Here";
 var serviceUrl = "https://maps.googleapis.com/maps/api/directions/json?origin="+origin+"&destination="+destination+
"&mode="+Maps.DirectionFinder.Mode.DRIVING+"&alternatives="+Boolean(1)+"&key="+Your_API_KEY;

 var options={
  muteHttpExceptions:true,
  contentType: "application/json",
 };

 var response=UrlFetchApp.fetch(serviceUrl, options);
  if(response.getResponseCode() == 200) {
   var directions = JSON.parse(response.getContentText());
    if (directions !== null){
     return directions;
     }
    }
  return false;
 }

因此,遍历代码......首先输入您的API密钥。 然后在var serviceUrl中选择您的参数。 我已经抛出了额外的参数(模式和替代方案)来展示如何添加它们。 如果您不想要它们,请将它们删除。

使用UrlFetch,您可以添加选项。 我已经使用了muteHttpExceptions,因此如果响应代码指示失败,则fetch不会抛出异常。 这样我们可以为函数选择一个返回类型,而不是抛出异常。 我正在使用JSON作为内容类型,因此我们可以使用相同的格式来发送和检索请求。 响应代码200表示成功,因此方向将解析并像getDirections()将返回的对象一样行动。 如果UrlFetch不成功(不同的响应代码)或者对象为null,则该函数将返回false。

当您查看Google Maps Directions API时,您将能够在开发者控制台中实时查看查询。 确保已启用结算,超出配额后将向您收取费用。

1.)我从控制台仪表板添加了一个API密钥。 请记住选择正在处理的正确项目。 https://console.developers.google.com/apis/credentials?project=

2.)在我的项目(脚本编辑器)中,我使用API​​密钥和控制台中的客户端ID将身份验证设置为映射。 我已经包含以下脚本:

function getDrivingDirections(startLoc, wayPoint, endLoc){
   var key = "Your_API_Key";
   var clientID = "Your_Client_ID";
   Maps.setAuthentication(clientID, key);
   var directions =  Maps.newDirectionFinder()
        .setOrigin(startLoc)
        .addWaypoint(wayPoint)
        .setDestination(endLoc)
        .setMode(Maps.DirectionFinder.Mode.DRIVING)
        .getDirections();
} return directions;

https://developers.google.com/apps-script/reference/maps/maps#setAuthentication(String,String)

自2017年7月13日起,我可以通过在“高级Google服务”菜单(图片1和2)以及Google Developer Console中启用Sheets API来启用API。 如果您使用相同的电子邮件地址登录Google表格,则无需提取功能。

[在“资源”菜单中,选择“高级Google服务”。] [1]

[在高级Google服务中,请确保已打开Goog​​le表格API。] [2]

[1]: [2]:

谢天谢地,JP Carlin! 谢谢你的回答。 JP的回答也解释了他的代码。 只是为了分享,没有代码解释(只需看看上面的JP Carlin的解释),下面是我的版本。 您将看到我还有departure_time参数,以便我可以获得特定日期时间的距离和驾驶分钟数。 我还添加了对Log错误的调用(在“View / Logs”下查看):

注意:Google支持人员告诉我, 支持在Google地图中使用Google地图的API密钥(例如使用“Directions API”)。 下面的代码有效,但是不受支持的解决方法。 截至11/4/2018,Google有一个内部故障单请求,要求在Google表格中添加对Google Maps API的支持,但没有添加该功能的时间表。

/********************************************************************************
 * directionsAPI: get distance and time taking traffic into account, from Google Maps API
********************************************************************************/
function directionsAPI(origin, destination, customDate) {
        var Your_API_KEY = "<put your APK key here between the quotes>";
        var serviceUrl = "https://maps.googleapis.com/maps/api/directions/json?origin="+origin+"&destination="+destination+"&departure_time="+customDate.getTime()+"&mode="+Maps.DirectionFinder.Mode.DRIVING+"&key="+Your_API_KEY;

        var options={
          muteHttpExceptions:true,
          contentType: "application/json",
         };

        var response=UrlFetchApp.fetch(serviceUrl, options);
          if(response.getResponseCode() == 200) {
           var directions = JSON.parse(response.getContentText());
            if (directions !== null){
             return directions;
             }
            }
      Logger.log("Error: " + response.getResponseCode() + " From: " + origin + ", To: " + destination + ", customDate: " + customDate + ", customDate.getTime(): " + customDate.getTime() );
      return false;
      }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM