我有一个Google Maps Engine项目,可以通过Google Forms / Google Apps Script更新数据源。我知道有一种方法可以在GAS中配置OAuth(https://developers.google.com/apps-script/reference/url-fetch/o-auth-config)但是我无法在花费数小时阅读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);
};
};
答案 0 :(得分:0)
这当然是可能的。 App脚本文档有一个tutorial,解释了如何使用以OA API为例的OAuth连接到远程服务。 This example还会显示正在执行的OAuth授权呼叫。
地图引擎教程的主要区别是第一步,您没有设置Twitter,而是在Developers Console中设置。
除了设置之外,这些指针可能对您有帮助。
UrlFetchApp.addOauthService("twitter")
来电可以使用任何字符串作为标识符,短语&#34; twitter&#34;没有什么特别之处,但需要匹配oAuthServiceName
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");
答案 1 :(得分:0)
为了我的目的有点太迟了,但我发现Google自己制作了library for GAS that enables OAuth 2.0。为什么这不包含在GAS中是超出我的。这看起来也很新,从5天前开始有一些更新。