【问题标题】:Printing spreadsheet to PDF then saving file in Drive using OAuth2将电子表格打印为 PDF,然后使用 OAuth2 将文件保存在云端硬盘中
【发布时间】:2015-06-02 02:52:29
【问题描述】:
  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 google-sheets


    【解决方案1】:

    您会发现一个功能可以为您的一张或全部工作表生成和保存 PDF Convert all sheets to PDF with Google Apps Script.

    对于 3 年前在 the cellar of the Local Planning Office 中未看到过 notice 的任何人,Google 已为其服务提供 deprecated OAuth1 和 OAuth1a 授权。

    在他们的指南Migrating from OAuthConfig to the OAuth1 library 中,Apps 脚本团队描述了如何将您的代码从一个迁移到另一个。他们没有提到的是你不需要

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

    您可以使用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 更复杂 - 但必然如此。

    【讨论】:

    • 甜蜜。我正要说......我对系统设计不是很了解,但是为什么即使我没有离开他们的环境,他们也会把这么简单的东西改成这么复杂的东西。我在他们的应用程序中并已登录,他们还需要什么身份验证?对我来说没有意义。我很高兴有更好的方法。
    • 顺便说一句,您在"muteHttpExceptions" 之前缺少,
    【解决方案2】:

    这里是变化。因为您使用的是 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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      相关资源
      最近更新 更多