【问题标题】:Using Google Script to send an email with a Word doc attachment使用 Google Script 发送带有 Word 文档附件的电子邮件
【发布时间】:2016-02-15 21:19:13
【问题描述】:

我有一个 google 脚本,可以发送带有 Word 文档作为附件的电子邮件。在谷歌弃用 OAuth 1.0 之前它一直有效

这是失败的那一行:

var doc = UrlFetchApp.fetch(url+'download/documents/Export?exportFormat=doc&format=doc&id='+ copyId, googleOAuth_('docs',url)).getBlob();

如果我删除第二个参数,即对 OAuth 的函数调用,它应该可以工作吗?为什么我需要进行身份验证?它应该能够使用来自谷歌驱动器的 ID 获取文档。它似乎可以工作(因为我没有看到任何错误),但是,当我收到一封电子邮件时,有一个损坏的 word doc 附件。

所以,我尝试实施 OAuth 2.0。但我没有得到任何地方。这是我的代码:

function getDriveService() {
  return OAuth2.createService('drive')

      .setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
      .setTokenUrl('https://accounts.google.com/o/oauth2/token')

      .setClientId(CLIENT_ID)
      .setClientSecret(CLIENT_SECRET)

      .setCallbackFunction('authCallback')

      .setPropertyStore(PropertiesService.getUserProperties())

      .setScope('https://www.googleapis.com/auth/drive')
      .setParam('login_hint', Session.getActiveUser().getEmail())
      .setParam('access_type', 'offline');
      //.setParam('approval_prompt', 'force');
}

function authCallback(request) {
  var driveService = getDriveService();
  var isAuthorized = driveService.handleCallback(request);
  if (isAuthorized) {
    return HtmlService.createHtmlOutput('Success! You can close this tab.');
  } else {
    return HtmlService.createHtmlOutput('Denied. You can close this tab');
  }
}

var oauth2Service = getDriveService();
var token = oauth2Service.getAccessToken();
var parameters = { method : 'get',
                    headers : {'Authorization': 'Bearer '+ token}};

var options =
    {
      "method" : "get"
    };    

var resp = UrlFetchApp.fetch('https://docs.google.com/feeds/download/documents/Export?exportFormat=doc&format=doc&id='+ copyId, parameters);
doc = resp.getBlob();

我收到一般错误 [未授予访问权限或已过期]。我想要的只是能够发送一封带有附件的电子邮件,该附件是从我的 Google 驱动器中存储的文档(格式 doc 或 docx)。似乎不可能!我可以将此文档附加为 pdf,但不是 Microsoft 文档。

任何帮助将不胜感激!

【问题讨论】:

  • 看来这个问题真的是关于从OAuth 1转换到OAuth 2。也许这就是标题应该说明的内容?

标签: oauth google-apps-script


【解决方案1】:

https://github.com/googlesamples/apps-script-oauth2 - 查看设置

...

您是否在库中添加了 OAuth 2.0?

资源 -> 库 -> 然后添加 'MswhXl8fVhTFUH_Q3UOJbXvxhMjh3Sh48'

【讨论】:

  • 什么是MswhXl8fVhTFUH_Q3UOJbXvxhMjh3Sh48
  • 就是从我附的github链接中添加OAuth库
猜你喜欢
  • 2021-11-17
  • 1970-01-01
  • 2012-07-14
  • 2015-09-09
  • 2012-06-15
  • 2017-09-06
相关资源
最近更新 更多