将电子表格打印为PDF,然后使用OAuth2将文件保存在驱动器中
我正在寻找使用OAuth2转换上述代码的分步教程。我在迁移时遇到了一些问题。我可以在OAuth2上找到一些代码,但不知道它们是如何联系在一起的。代码以前很简单,现在看起来复杂多了?还是我错过了一些简单的东西将电子表格打印为PDF,然后使用OAuth2将文件保存在驱动器中,pdf,google-apps-script,google-sheets,Pdf,Google Apps Script,Google Sheets,我正在寻找使用OAuth2转换上述代码的分步教程。我在迁移时遇到了一些问题。我可以在OAuth2上找到一些代码,但不知道它们是如何联系在一起的。代码以前很简单,现在看起来复杂多了?还是我错过了一些简单的东西 我试图替换OAuth连接部分,但遇到了问题。似乎应该以某种方式使用getDriveService?以下是更改。因为您使用的是DriveApp,所以您的脚本已经有权从任何源(包括UrlFetchApp)访问您的文件。您所要做的就是从脚本中获取令牌,并将其传递到Fetch请求的头中 func
我试图替换OAuth连接部分,但遇到了问题。似乎应该以某种方式使用getDriveService?以下是更改。因为您使用的是DriveApp,所以您的脚本已经有权从任何源(包括UrlFetchApp)访问您的文件。您所要做的就是从脚本中获取令牌,并将其传递到Fetch请求的头中
function topdf() {
var foldersave=DriveApp.getFolderById('0Byy1DdsfdfTQRnVlfb05wOV83T00')
var d= new Date()
var oauthConfig = UrlFetchApp.addOAuthService("google");
var scope = "https://docs.google.com/feeds/";
//make OAuth connection
oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oauthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oauthConfig.setConsumerKey("anonymous");
oauthConfig.setConsumerSecret("anonymous");
//get request
var request = {
"method": "GET",
"oAuthServiceName": "google",
"oAuthUseToken": "always",
"muteHttpExceptions": true
};
var key='1QUj_OyHisdfsdfjwfNu1l-JuI528ev6FNRJv-oljIY';
var fetch='https://docs.google.com/spreadsheets/d/'+key+'/export?format=pdf&size=A4&portrait=false'
var name = "Timestamp for: "+ d + ".pdf";
var pdf = UrlFetchApp.fetch(fetch, request);
pdf = pdf.getBlob().setName(name);
var file = foldersave.createFile(pdf)
}
您将在中找到一个函数,用于生成和保存一张或所有工作表的PDF 对于那些3年前还没有看到这篇文章的人来说,谷歌已经为他们的服务提供了OAuth1和OAuth1a授权 在他们的指南中,应用程序脚本团队描述了如何将代码从一个迁移到另一个。他们没有提到的是你不需要 至少对于访问谷歌的服务来说,有一种更简单的方法 您可以通过获取当前用户的OAuth 2.0访问令牌,这意味着对以前使用OAuthConfig的任何脚本进行简化更改 要转换脚本,请执行以下操作: 替换
function topdf() {
var foldersave=DriveApp.getFolderById('0Byy1DdsfdfTQRnVlfb05wOV83T00');
var d= new Date();
var request = {
"method": "GET",
"headers":{"Authorization": "Bearer "+ScriptApp.getOAuthToken()},
"muteHttpExceptions": true
};
var key='1QUj_OyHisdfsdfjwfNu1l-JuI528ev6FNRJv-oljIY';
var fetch='https://docs.google.com/spreadsheets/d/'+key+'/export?format=pdf&size=A4&portrait=false'
var name = "Timestamp for: "+ d + ".pdf";
var pdf = UrlFetchApp.fetch(fetch, request);
pdf = pdf.getBlob().setName(name);
var file = foldersave.createFile(pdf)
}
与
删除对旧OAuthConfig类的所有剩余引用
var request = {
"method": "GET",
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
},
"muteHttpExceptions": true
};
就这些
如果您使用的是需要OAuth 2.0身份验证的外部非Google服务,请遵循迁移指南
是的,即使有了这个库,它也比OAuth1更复杂——但这是必然的。Sweet。我正要说。。。我对系统设计不是很了解,但为什么他们要把这么简单的东西变成这么复杂的东西,即使我没有离开他们的环境。我在他们的应用程序中并已登录,他们还需要什么身份验证?对我来说没有意义。我很高兴有更好的方法。顺便说一句,你错过了一个,在muteHttpExceptions之前
var request = {
"method": "GET",
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
},
"muteHttpExceptions": true
};
...
var oauthConfig = UrlFetchApp.addOAuthService("google");
var scope = "https://docs.google.com/feeds/";
//make OAuth connection
oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oauthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oauthConfig.setConsumerKey("anonymous");
oauthConfig.setConsumerSecret("anonymous");
...