【发布时间】: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。也许这就是标题应该说明的内容?