序列化继续时出现意外异常

时间:2012-06-08 18:21:49

标签: google-docs-api google-apps-script urlfetch

我收到此错误:序列化继续时出现意外异常(帮助不大)

它是由FetchUrlApp.fetch()引起的;呼叫。我使用Google Apps脚本网站,而不是Google Spreadsheets。代码在原始实例中工作,但只要我将代码复制并粘贴到新项目中,我就会收到上述错误消息。我正在访问Google Docs API。我在其他论坛上看到我需要授权,但我无法获得正确的代码授权。当我第一次运行代码副本时,没有提示弹出。

代码施加:

var oauthConfig = UrlFetchApp.addOAuthService("docs");
oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope=https://docs.google.com/feeds/");
oauthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oauthConfig.setConsumerKey(_consumerKey_);
oauthConfig.setConsumerSecret(_consumerSecret_);

var requestData3 = {
  "method": "GET",
  "headers": {"GData-Version": "3.0"},
  "oAuthServiceName": "docs",
  "oAuthUseToken": "always",
};

var url = "https://docs.google.com/feeds/" + userName + "/private/full/-/mine"; 
var result = UrlFetchApp.fetch(url, requestData3); //error occurs, any thoughts?

提前谢谢你, 詹姆斯克里姆

2 个答案:

答案 0 :(得分:0)

我建议写一个特殊的函数,除了调用Oauth进程并从脚本编辑器调用它之外什么都不做。 例如,这是我最近用来制作授权弹出窗口的那个:

function authorize(){
    // function to call from the script editor to authorize googleOauth
    var id=mailtemplatedoc
      var url = 'https://docs.google.com/feeds/';
      var doc = UrlFetchApp.fetch(url+'download/documents/Export?  exportFormat=html&format=html&id='+id,
 googleOAuth_('docs',url)).getContentText();  
    }
编辑:我遗忘的遗漏部分:

function googleOAuth_(name,scope) {
  var oAuthConfig = UrlFetchApp.addOAuthService(name);
  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('anonymous');
  oAuthConfig.setConsumerSecret('anonymous');
  return {oAuthServiceName:name, oAuthUseToken:"always"};
}

答案 1 :(得分:0)

您必须将consumerKey和consumerSecret都设置为“匿名”才能触发三方OAuth流程:

oauthConfig.setConsumerKey("anonymous");
oauthConfig.setConsumerSecret("anonymous");

用这些替换你的两行,并显示授权弹出对话框,允许用户授予对其文档的访问权。