1

我的问题涉及使用一种解决方法来解决将 Google Doc 转换为 HTML 以用作Henrique Abreu发布的电子邮件模板的问题。它在 Google Apps 脚本问题跟踪器上注册为问题 585 。

一年中的大部分时间我一直在使用以下代码,除了丑陋的授权场景(即自动授权系统不起作用)之外,它工作正常。

function getDocAsHtml(docId){
  var url = 'https://docs.google.com/feeds/download/documents/Export?exportFormat=html&format=html&id=';
  var auth = googleOAuth_('docs',url+docId);
  return UrlFetchApp.fetch(url+docId,auth).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]。

问题是,以前当我授予权限时,调试器授权对话框会消失并且系统会正常工作,而现在该对话框只是重新出现。当尝试从电子表格中的菜单运行代码路径时,我得到一个通用的“糟糕!需要授权”对话框 [1]。

撇开对于一个已经存在很长时间的问题来说,这是一个丑陋的解决方法这一事实,有什么改变使这个解决方法不再有效?还有其他解决方案吗?

[1] 不幸的是,我还不能上传图片,但可以查看问题跟踪器,其中我附上了一张图片,其中包含我正在谈论的错误和授权对话框的示例。

4

2 回答 2

1

我看到您在问题跟踪器上发帖并在新的电子表格上进行了一些测试。在我的测试中,当我使用这个小功能时,授权过程就会出现:

function autorise(){
// fonction à appeler pour autoriser googleOauth
var id= "put here the string ID of a doc you own"
  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();  
}
// this part is the same you use and is already in your script... I show it here for info only
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"};
}

也许你可以试试?

或者,如果您愿意,可以在此共享测试表上进行测试

于 2012-06-09T13:12:04.323 回答
1

在您的问题(和您的示例电子表格)中,您使用的范围我不确定是否会起作用。我用于文档的“范围”是“https://docs.google.com/feeds/”。这记录在这里https://developers.google.com/gdata/docs/auth/oauth#Scope。请在您的代码中更改您传递给 googleOAuth_ 的范围,并让我们知道问题是否消失。

于 2012-06-10T05:57:00.390 回答