获得谷歌的Apps脚本授权融合表API(Getting Google Apps Script to

2019-07-20 04:17发布

我建设,我希望能在一个融合表来承载一个数据库,我正在开发一些测试功能与融合API接口。 我使用谷歌Apps脚本,并严重依赖于其他来源的代码。 我大部分时间都在一天研究这个问题,现在我卡住了。

当脚本编辑器运行addNewColumn(),它得到了“获取就绪”日志项(因为我可以在日志中看到),它再通过认证过程(每次),然后就停止变(和永远不会获取在“魂执行”日志条目)。 有没有引发的错误但它从来没有完成取指令。

在调试模式下运行时,它会通过授权,然后在取线挂起。 如果我点击“一步式”我得到的只是说“OAuth错误”的错误。 我真的不知道这里做什么。 任何帮助将非常感激。

function googleAuth() {
      var scope = "https://www.googleapis.com/auth/fusiontables";
      var service = 'fusion';
      var oAuthConfig = UrlFetchApp.addOAuthService(service);
      oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/"+
                 "OAuthGetRequestToken?scope="+scope);
      oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
      oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
      oAuthConfig.setConsumerKey('{consumer key}');
      oAuthConfig.setConsumerSecret('{secret}');
      return {oAuthServiceName:'fusion', oAuthUseToken:"always"};
}

function addNewColumn(){   
      var p = {
          'name' : "newColumn",
          'type' : "NUMBER"
      };

      Logger.log(addColumn(p));
}


function addColumn(parameters) {
  var url = "https://www.googleapis.com/fusiontables/v1/";
  var fetchArgs = googleAuth(); 

  fetchArgs.method = "POST";
  fetchArgs.payload = parameters;

  //if the fetch arg payload is set to null it will add an unnamed column
  //fetchArgs.payload = null;  

  url += "tables/"+"{table id}"+"/columns";
  url += '?key='+"{key}";

  Logger.log("fetch ready");

  var result = UrlFetchApp.fetch(url, fetchArgs).getContentText();

  Logger.log("fetch executed");

  return Utilities.jsonParse(result);  
}

理解

我已经简化我的功能测试,我已经发现,通过在期权体的有效域简单地插入“空”来代替“有效载荷”变量,代码将成功地创建在融合表中的无名列。 然而,当我重新插入有效载荷变量 - 它停止工作。

不工作的时候,它要么:要求重新授权每次我从编辑器运行时,或者从一个Web应用程序的状态运行时“遇到错误:需要授权才能执行该操作。” 如何有效载荷变化的授权? 我剪切并粘贴完全相同的有效载荷送入了OAuth2.0的操场上和它完美的作品。 请帮忙。

function googleAuth() {
      var oAuthConfig = UrlFetchApp.addOAuthService(service);
      oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/"+
                 "OAuthGetRequestToken?scope=" + scope);
      oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
      oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
      oAuthConfig.setConsumerKey(clientId);
      oAuthConfig.setConsumerSecret(clientSecret);
      return {oAuthServiceName:service, oAuthUseToken:"always"};
}

function addColumn() {
  googleAuth();
  var payload = 
    {
      name : "IT WORKED",
      type : "STRING"
    };
  var options =
    {
      "oAuthUseToken":"always",
      "oAuthServiceName":service,
      "method" : "post",
      "payload":payload
    };
  var url = fetchUrl + "tables/" + fusionId + "/columns" + "?key=" + apiKey;
  var fetchResult = UrlFetchApp.fetch(url, options);
  Logger.log(fetchResult.getContentText());
  return Utilities.jsonParse(fetchResult.getContentText());
}

更新#2

我已经找到一种方法,让它运行没有任何错误,但它仍然不分配请求的名称和类型的新列。 最近除了被指定内容类型,如下所示。 通过强制的contentType为“应用程序”或“JSON”,它将运行,但仍然没有通过有效载荷预期。

  var options =
    {
      "oAuthUseToken":"always",
      "oAuthServiceName":service,
      "method" : "POST",
      "payload" : payload,
      'contentType' : 'application'
    };

Answer 1:

我有同样的问题,试图获得和处理来自融合表中的数据。

有一个在谷歌的著名脚本库的融合表库: https://developers.google.com/apps-script/notable-script-libraries?hl=pt-BR

我挂了图书馆,但它似乎是马车。 所以(用于调试的原因),我复制的源代码,我的项目,并得到了它的工作:

https://sites.google.com/site/scriptsexamples/new-connectors-to-google-services/fusion-tables-class/fusion-source

也许你没有找到答案。



Answer 2:

解决了!!! 该解决方案是双重的:

  1. 有效负载有通过JSON.stringify被转换成字符串()
  2. 将contentType不得不手动设置为“application / JSON”

这比多教程,我在网上找到的,但想分享我工作的解决方案非常不同。

function addColumn(authToken) {
  googleAuth();
  var payload =
    {
      name : 'YES',
      type : 'STRING'
    };
  var options =
    {
      payload : JSON.stringify(payload),
      oAuthUseToken : "always",
      oAuthServiceName : service,
      method : "POST",
      contentType : "application/json"
    };
  var url = fetchUrl + "tables/" + fusionId + "/columns";// + "?key=" + apiKey;
  var fetchResult = UrlFetchApp.fetch(url, options);
  Logger.log(fetchResult.getHeaders());
  Logger.log(fetchResult.getContentText());
  return Utilities.jsonParse(fetchResult.getContentText());

}


Answer 3:

  • 这似乎是不同的,其它的URLFetch POST实例是因为融合表服务预计的实际有效载荷JSON作为请求“BODY”的原因。 当添加JSON对象(没有字符串化),为“有效载荷”,UrlFetchApp将每个名称/值对转换成URL编码请求字符串(即名称= YES&类型= STRING)。 然而,当有效载荷是一个字符串(如你正在使用字符串化的情况下),网址抓取只会URL编码所提供的字符串(在这种情况下,JSON字符串)。 如果你是“投稿”的服务预期的名称/值参数,那么你就不会使用字符串化。
  • 注意:只要你知道,并澄清自己发布的其他相关问题, 在这里 ,你在这个例子中使用OAuth 1.0(addOAuthService只支持Oauth1.0端点和协议)
  • 还要说明一点:你是混合Utilities.jsonParse和JSON.stringify。 我会用JSON.parse一致。


文章来源: Getting Google Apps Script to Authorize Fusion Table API