在脚本编辑器中设置Maps API密钥 [英] Set Maps API key in Script Editor

查看:113
本文介绍了在脚本编辑器中设置Maps API密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As far as I understand, in order to track our quota usage, we need to provide our API key to the Google App Service on the service we are planning to use.

在我的情况下,我有一个包含起点和目的地的电子表格以及一个自定义函数来计算两者之间的距离。

In my case I have a spreadsheet with Origin and Destination and a Custom function to calculate the distance between.

我遇到了无法通过调用来达到配额的问题 .getDirections()

I ran into the problem of meeting the quota from invoking .getDirections():


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

Error: Service invoked too many times for one day: route. (line **).

代码示例:

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

所以我读到,如果我将API密钥分配给我的项目,我应该

So I read that if I assign the API Key to my project I should be able to see the usage and how close to the free quota I am.

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

In the script editor, I did enable all of the APIs under Resources menu/ Advanced Google Services. Then I went to the Google Developers Console and there I did not see any record of how many times my custom function called the Google Maps API or any API usage.

从逻辑上讲,我认为在脚本中我需要设置google API密钥,以便脚本开始使用用户名调用API并计算使用的时间某些API。我猜现在我正在以匿名方式使用Google Maps API,并且由于整个公司都分配了相同的IP,因此我们用尽了允许的号码来调用此函数。

Logically I think that in my script I need to set my google API Key so my scripts start to call the API under my user name and count the number of time I used certain API. I guess right now I am using the Google Maps API as anonymous and since the whole company is assigned with the same IP, so we exhaust the permitted numbers to call this function.

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

Bottom line please reply if you know a way to connect my simple Spreadsheet function to the Public API access Key I have.

谢谢,
保罗

推荐答案

我也很想找到这个答案很长时间了很高兴地说我找到了它。 Google似乎已根据日期此页面已更新。

I also have been eager to find this answer for a long time and am happy to say that I've found it. It looks like Google might have just made this available around Oct 14, 2015 based on the date this page was updated.

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

You can leverage the UrlFetchApp to add your API key. The link I posted above should help with obtaining that key.

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中选择参数。我添加了其他参数(模式和替代方法)以说明如何添加它们。如果您不想要它们,请将其删除。

So walking through the code... first put in your API key. Then choose your parameters in the var serviceUrl. I've thrown in additional parameters (mode and alternatives) to show how you can add them. If you don't want them, remove them.

使用UrlFetch,您可以添加选项。我使用了MutantHttpExceptions,以便如果响应代码指示失败,则提取不会引发异常。这样,我们可以为函数选择返回类型,而不是引发异常。我使用JSON作为内容类型,因此我们可以使用相同的格式发送和检索请求。响应代码200表示成功,因此路线将进行解析并像getDirections()返回的对象一样起作用。如果UrlFetch不成功(其他响应代码)或对象为null,则该函数将返回false。

With UrlFetch you can add options. I've used muteHttpExceptions so that the fetch will not throw an exception if the response code indicates failure. That way we can choose a return type for the function instead of it throwing an exception. I'm using JSON for the content type so we can use the same format to send and retrieve the request. A response code of 200 means success, so directions will then parse and act like the object that getDirections() would return. The function will return false if the UrlFetch was not successful (a different response code) or if the object is null.

当您查看Google Maps Directions API时,便可以在开发人员控制台中实时查看查询。确保已启用结算功能,超过配额后将向您收费。

You will be able to see the queries in real time in your developer console when you look in the Google Maps Directions API. Be sure that billing is enabled, and you will be charged once you exceed the quotas.

这篇关于在脚本编辑器中设置Maps API密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆