簡體   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