【问题标题】:OAuth 2.0 Authorization: GAS and Google Maps EngineOAuth 2.0 授权:GAS 和谷歌地图引擎
【发布时间】:2015-03-10 05:22:38
【问题描述】:

我有一个 Google Maps Engine 项目,可以通过 Google Forms/Google Apps 脚本更新数据源。我知道有一种方法可以在 GAS (https://developers.google.com/apps-script/reference/url-fetch/o-auth-config) 中配置 OAuth,但是在花费数小时阅读 GAS 和 GME 文档后,我无法弄清楚如何使其工作。我已经能够使用OAuth Playground 获得访问令牌来解决它,但我需要每小时手动刷新一次。我知道答案可能很简单,但我是 OAuth 新手,找不到可以帮助我的简单指南。

如何让我的 Google Apps 脚本通过 OAuth 与 Google Maps Engine 完美配合?

我在下面列出了我目前访问 GME 的方式:

    /* This function is called when a new provider is added through the "Medical Providers" form
   It sends an HTTP request to Google Maps Engine to add the new provider to the map */
function addNewtoTable(row){
  var aPIKey = "MY_API_KEY";
  var bearer = "ACCESS_TOKEN_FROM_OAUTH_PLAYGROUND";
  var projectID = "MY_PROJECT_ID";
  var tableID = "MY_TABLE_ID";
  //tutorial here https://developers.google.com/maps-engine/documentation/tutorial
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Providers");

  var address = sheet.getRange(row,2).getValue();
  var response = Maps.newGeocoder().geocode(address);
  for (var j = 0; j < response.results.length; j++) {
    var result = response.results[j];
    //Logger.log('%s: %s, %s', result.formatted_address, result.geometry.location.lat,
    //             result.geometry.location.lng);
  };
  var lat = result.geometry.location.lat;
  var long = result.geometry.location.lng;
  var name= '"'+sheet.getRange(row,1).getValue()+'"';
  var phone= '"'+sheet.getRange(row,4).getValue().toString()+'"';
  var email= '"'+sheet.getRange(row,3).getValue()+'"';
  var inbounds= '"'+sheet.getRange(row,5).getValue().toString()+'"';
  var outbounds = '"'+sheet.getRange(row,6).getValue().toString()+'"';
  var lastIn = '" '+sheet.getRange(row,7).getValue().toString()+' "';
  var lastOut = '" '+sheet.getRange(row,8).getValue().toString()+' "';
  var gxid = '"'+sheet.getRange(row,9).getValue().toString()+'"';

  //HTTP request goes here
  var payload = '{features:[{type: "Feature",geometry:{type: "Point",coordinates: ['+long+','+lat+']},properties: {gx_id: '+gxid+',name: '+name+',phone:'+phone+',email:'+email+',inbound:'+inbounds+',outbound:'+outbounds+',last_inbound:'+lastIn+',last_outbound:'+lastOut+'}}]}';
  Logger.log(payload);


  var headers = {"Authorization": "Bearer ACCESS_TOKEN_FROM_OAUTH_PLAYGROUND", "Content-type": "application/json"};
  var options ={"method" : "post","headers" : headers, "payload" : payload, "muteHttpExceptions" : true};
  var httpresponse = UrlFetchApp.fetch("https://www.googleapis.com/mapsengine/v1/tables/MY_TABLE_ID/features/batchInsert",options);
  Logger.log(httpresponse);

  if (httpresponse!=""){
    MailApp.sendEmail('MY_EMAIL', 'HTTP Request Failed to Send', httpresponse);
  };
};

【问题讨论】:

标签: oauth google-apps-script google-maps-engine google-maps-engine-lite


【解决方案1】:

这当然是可能的。 App Script 文档有一个tutorial,解释了如何使用使用 Twitter API 作为示例的 OAuth 连接到远程服务。 This example 还显示正在执行 OAuth 授权调用。

Maps Engine 教程的主要区别在于第一步,您无需在 Twitter 上进行设置,而是在 Developers Console 中进行设置。

  • 您想在 APIs & Auth -> Credentials 下创建一个新的 OAuth 客户端 ID。这是一个网络应用程序。
  • 创建客户端 ID 时,您将在控制台中设置“授权重定向 URI”,而不是在 Twitter 中设置“回调 URL”。将授权来源也设置为 docs.google.com,以防万一。
  • 您也可以通过 console.developers.google.com 获得您的“Consumer Key”和“Consumer Secret”,它们对应于 this GME doc 中提到的 Client ID 和 Client Secret。

除了设置之外,这些指针可能会对您有所帮助。

  • UrlFetchApp.addOauthService("twitter") 调用可以使用任何字符串作为标识符,短语“twitter”没有什么特别之处,但它需要匹配oAuthServiceName
  • 您需要的 URL 看起来应该是这些(从 here 获取):
    • oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
    • oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope); 范围解释 here
    • oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");

【讨论】:

    【解决方案2】:

    对我的目的来说有点太晚了,但我发现谷歌自己做了一个library for GAS that enables OAuth 2.0。为什么这不包含在 GAS 中,我无法理解。这看起来也很新,5 天前有一些更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多