【问题标题】:Import JSON from app engine to google spreadsheet将 JSON 从应用引擎导入到谷歌电子表格
【发布时间】:2014-01-23 15:16:07
【问题描述】:

我正在尝试将应用引擎应用程序中的数据(json 格式)导入 Google 电子表格。

我们需要通过我们的 Google(企业应用程序)帐户进行身份验证才能访问 json。

现在,我只是想将内容放入一个单元格中。 所以,按照谷歌文档(https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app),我尝试了这个简单的代码:

function getCC() {

// Setup OAuthServiceConfig

var oAuthConfig = UrlFetchApp.addOAuthService('google');  
oAuthConfig.setAccessTokenUrl('https://app-id.appspot.com/_ah/OAuthGetAccessToken');  
oAuthConfig.setRequestTokenUrl('https://app-id.appspot.com/_ah/OAuthGetRequestToken');  
oAuthConfig.setAuthorizationUrl('https://app-id.appspot.com/_ah/OAuthAuthorizeToken'); 
oAuthConfig.setConsumerKey('app-id.appspot.com');  
oAuthConfig.setConsumerSecret('ConsumerSecret');  

var options =
{
"oAuthServiceName" : 'google',
"oAuthUseToken" : 'always',
"method": 'GET'
};

var resp = UrlFetchApp.fetch('https://app-id.appspot.com/rest/na/customer/all', options);  

// clear everything in the sheet
SpreadsheetApp.getActiveSheet().clearContents().clearFormats();

// set the values in the sheet (as efficiently as we know how)
SpreadsheetApp.getActiveSheet().getRange(1, 1).setValue(resp);  

}

我收到了这个错误: Request failed for returned code 302. Truncated server response: (use muteHttpExceptions option to examine full response) (line 19, file "basic")

所以我遵循了这个: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3478 但我在日志中有完全相同的错误消息:/

编辑

这里有 HttpmuteExceptions = true :

function getCC() {

var oAuthConfig = UrlFetchApp.addOAuthService('google');  
oAuthConfig.setAccessTokenUrl('https://app-id.appspot.com/_ah/OAuthGetAccessToken');  
oAuthConfig.setRequestTokenUrl('https://app-id.appspot.com/_ah/OAuthGetRequestToken');  
oAuthConfig.setAuthorizationUrl('https://app-id.appspot.com/_ah/OAuthAuthorizeToken'); 
oAuthConfig.setConsumerKey('app-id.appspot.com');  
oAuthConfig.setConsumerSecret('ConsumerSecret');    

var fetchParameters = {};
fetchParameters.oAuthServiceName = 'google';
fetchParameters.oAuthUseToken = 'always';
fetchParameters.method = 'GET';
fetchParameters.muteHttpExceptions = true

      try {
    var url = 'https://app-id.appspot.com/oauth/na/customer/all';
    var result = UrlFetchApp.fetch(url, fetchParameters);
    SpreadsheetApp.getActiveSheet().clearContents().clearFormats();
    SpreadsheetApp.getActiveSheet().getRange(1, 1).setValue(result);  
      } catch (e) {
    Logger.log(e);
      }

}

日志输出:

[14-01-27 11:48:33:692 CET] getResponseCode: function getResponseCode() {/*
*/}

[14-01-27 11:48:33:692 CET] getContentText: function getContentText() {/*
*/}

[14-01-27 11:48:33:692 CET] getContent: function getContent() {/*
*/}

[14-01-27 11:48:33:693 CET] getAs: function getAs() {/*
*/}

[14-01-27 11:48:33:693 CET] getHeaders: function getHeaders() {/*
*/}

[14-01-27 11:48:33:693 CET] toString: function toString() {/*
*/}

[14-01-27 11:48:33:693 CET] getBlob: function getBlob() {/*
*/}

[14-01-27 11:48:33:693 CET] getAllHeaders: function getAllHeaders() {/*
*/}

您知道如何解决这个问题吗?

谢谢

【问题讨论】:

  • 你能在选项 json 中将 muteHttpExceptions 设置为 true 并粘贴回响应的内容吗?
  • 结果如下:pastebin.com/kmg6wPrA

标签: json google-app-engine google-apps-script


【解决方案1】:

成功了。

我使用了相同的代码并将其放在另一个脚本文件中。我认为 Oauth 保留旧信息存在问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多