将JSON从应用引擎导入谷歌电子表格

时间:2014-01-23 15:16:08

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

我正在尝试将数据(以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() {/*
*/}

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

由于

1 个答案:

答案 0 :(得分:0)

有效。

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