脚本编辑器中的API密钥 [英] API key in Script Editor

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

问题描述

据我所知,为了追踪我们的报价使用情况,我们需要在我们计划使用的服务上向Google应用服务提供API密钥。



在我的例子中,我有一个包含Origin和Destination以及Custom函数的电子表格来计算两者之间的距离。



我遇到了引用.getDirections()
Erorr:服务在一天中调用的次数太多:route。(line **)。

代码示例:

  function getDirections_(origin,destination){
var directionFinder = Maps.newDirectionFinder();
directionFinder.setOrigin(origin);
directionFinder.setDestination(destination);
var directions = directionFinder.getDirections();
返回方向;





$ b

因此,我读到如果将API密钥分配给我的项目,我应该能够看到使用情况,以及与我的免费报价有多接近。



在脚本编辑器中,我确实启用了所有API的资源菜单/高级Google服务。然后我去了Google Developers Console,那里有
,我没有看到我的自定义函数被称为Google Maps API或任何API使用情况的记录。从逻辑上讲,我认为在我的脚本中,我需要设置我的Google API Key,以便我的脚本开始在我的用户名下调用API,并计算我使用的次数某些API。我想现在我以匿名的方式使用Google Maps API,并且由于整个公司都被分配了相同的IP地址,所以我们用尽了许多号码来调用此函数。



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



谢谢你,
Paul

解决方案

我也一直渴望长时间找到这个答案并且很高兴地说我已经找到了它。看起来Google可能会在2015年10月14日前根据日期此页面已更新。

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

 函数说明API(origin,destination){
var Your_API_KEY =将您的API密钥放在这里;
var serviceUrl =https://maps.googleapis.com/maps/api/directions/json?origin=\"+origin+\"&destination=\"+destination+
& mode =+ Maps.DirectionFinder.Mode.DRIVING + &安培;替代= +布尔(1)+ &安培;关键= + 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){
返回方向;
}
}
返回false;
}

因此,通过代码......首先放入您的API密钥。然后在var serviceUrl中选择你的参数。我已经引入了其他参数(模式和替代方法)以显示如何添加它们。如果你不想要它们,请删除它们。

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

当您查看Google Maps Directions API时,您将能够在开发者控制台中实时查看查询。请确保帐单已启用,并且一旦您超出配额,您将收取费用。


As far as I understand, in order to track our quote 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.

I ran into the problem of meeting the quote from invoking " .getDirections() " Erorr: " Service invoked too many times for one day: route. (line **). "

Sample of the code:

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

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 quote I am.

In the script editor, I did enable all of the APIs unter 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.

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.

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

Thank you, Paul

解决方案

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.

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;
 }

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.

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.

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.

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

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