使用 OAuth2 将电子表格打印为 PDF,然后将文件保存在云端硬盘中 [英] Printing spreadsheet to PDF then saving file in Drive using OAuth2

查看:31
本文介绍了使用 OAuth2 将电子表格打印为 PDF,然后将文件保存在云端硬盘中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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 上找到一些代码,但不知道它是如何联系在一起的.之前的代码真的很简单,现在好像复杂多了?还是我遗漏了一些简单的东西?

I'm looking for a step by step tutorial to convert the above code using OAuth2 . I'm having some problems migrating. I can find bits of code on OAuth2, but don't know how it ties together. The code was really simple before, now it seems to be much more complicated? Or am I missing something simple?

我已尝试替换 OAuth 连接部分,但遇到了问题.https://github.com/googlesamples/apps-script-oauth2getDriveService 应该以某种方式使用吗?

I've tried to replace the OAuth connection section but having trouble. https://github.com/googlesamples/apps-script-oauth2 it seems like the getDriveService should be used somehow?

推荐答案

您会在使用 Google Apps 脚本将所有工作表转换为 PDF.

对于没有看过 通知张贴在地方规划办公室的地窖 3 年以前,谷歌已经弃用 OAuth1 &对其服务的 OAuth1a 授权.

For anyone who hadn't seen the notice posted in the cellar of the Local Planning Office 3 years ago, Google has deprecated OAuth1 & OAuth1a authorization for their services.

在他们的指南中,从 OAuthConfig 迁移到 OAuth1 库,Apps 脚本团队介绍了如何将您的代码从一个迁移到另一个.他们没有提到的是你不需要.

In their guide, Migrating from OAuthConfig to the OAuth1 library, the Apps Script team describes how to migrate your code from one to the other. What they fail to mention is that you don't need to.

有一种更简单的方法,至少对于访问 Google 的服务是这样.

There IS an easier way, at least for accessing Google's services.

您可以使用 ScriptApp.getOAuthToken(),这意味着对以前使用 OAuthConfig 的任何脚本进行了简化更改.

You can obtain the OAuth 2.0 access token for the current user with ScriptApp.getOAuthToken(), which means a simplifying change in any script that previously used OAuthConfig.

要转换您的脚本:

  1. 替换

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

var request = {
  "method": "GET",
  headers: {
    'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken()
  },
  "muteHttpExceptions": true
};

  • 删除对旧 OAuthConfig 类的所有剩余引用.

  • Delete every remaining reference to the old OAuthConfig class.

    ...
    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)服务,请遵循迁移指南.

    Follow the migration guide if you're using an external (non-Google) service that requires OAuth 2.0 authentication.

    是的,即使使用库,它也比 OAuth1 复杂 - 但必然如此.

    And yes, even with the library it's more complicated than OAuth1 was - but necessarily so.

    这篇关于使用 OAuth2 将电子表格打印为 PDF,然后将文件保存在云端硬盘中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆