google apps脚本-将电子表格打印成PDF,然后使用OAuth2将文件保存在驱动器中


  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)
}

我正在寻找一个循序渐进的教程,使用OAuth2转换上述代码。我在迁徙时遇到了一些问题。我可以在OAuth2上找到一些代码,但不知道它是如何联系在一起的。代码以前很简单,现在看起来复杂多了。还是我错过了一些简单的东西?

我试图替换OAuth连接部分,但遇到麻烦。https://github.com/googlesamples/apps-script-oauth2看起来getDriveService应该以某种方式使用?

您将找到一个函数,该函数为您的一个或所有工作表生成并保存pdf使用Google Apps Script将所有工作表转换为PDF。

对于那些没有看到三年前张贴在当地规划办公室地下室的通知的人来说,谷歌已经弃用了OAuth1 &OAuth1a对其服务的授权。

Apps Script团队在他们的指南《从OAuthConfig迁移到OAuth1库》中描述了如何将代码从一个迁移到另一个。他们没有提到的是你不需要

有一种更简单的方法,至少可以访问谷歌的服务。

您可以使用ScriptApp.getOAuthToken()获取当前用户的OAuth 2.0访问令牌,这意味着对以前使用OAuthConfig的任何脚本进行简化更改。

转换脚本:

  1. 替换
    var request = {
      "method": "GET",
      "oAuthServiceName": "google",
      "oAuthUseToken": "always",
      "muteHttpExceptions": true
    };
    

    var request = {
      "method": "GET",
      headers: {
        'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken()
      },
      "muteHttpExceptions": true
    };
    
  2. 删除所有对旧OAuthConfig类的引用。

    ...
    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");
    ...
    

这就是它的全部。

如果您正在使用需要OAuth 2.0身份验证的外部(非google)服务,请遵循迁移指南。

是的,即使使用库,它也比OAuth1更复杂——但这是必然的。

变化如下:因为你使用的是DriveApp,你的脚本已经有权从任何来源访问你的文件,包括UrlFetchApp。您所要做的就是从脚本中获取令牌,并将其传递到Fetch请求的头中。

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)
}

相关内容

最新更新