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

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

问题描述

 函数topdf(){
var foldersave = DriveApp.getFolderById('0Byy1DdsfdfTQRnVlfb05wOV83T00')
var d = new Date()

var oauthConfig = UrlFetchApp.addOAuthService(google);
var scope =https://docs.google.com/feeds/;

//使OAuth连接
oauthConfig.setAccessTokenUrl(https://www.google.com/accounts/OAuthGetAccessToken);
oauthConfig.setRequestTokenUrl(https://www.google.com/accounts/OAuthGetRequestToken?scope=+范围);
oauthConfig.setAuthorizationUrl(https://www.google.com/accounts/OAuthAuthorizeToken);
oauthConfig.setConsumerKey(anonymous);
oauthConfig.setConsumerSecret(anonymous);

//获取请求
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



对于没有看到 notice 发布在地方规划办公室的地下室 3年前,Google有弃用d OAuth1& OAuth1a授权他们的服务。



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

有一种更简单的方法,至少对于访问Google的服务。 / p>

您可以通过 ScriptApp.getOAuthToken() ,这意味着以前使用OAuthConfig 。



转换您的脚本:

替换

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

with

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


  • 删除旧的OAuthConfig类的其余引用。

      ... 
    var oauthConfig = UrlFetchApp.addOAuthService(google);
    var scope =https://docs.google.com/feeds/;

    //使OAuth连接
    oauthConfig.setAccessTokenUrl(https://www.google.com/accounts/OAuthGetAccessToken);
    oauthConfig.setRequestTokenUrl(https://www.google.com/accounts/OAuthGetRequestToken?scope=+范围);
    oauthConfig.setAuthorizationUrl(https://www.google.com/accounts/OAuthAuthorizeToken);
    oauthConfig.setConsumerKey(anonymous);
    oauthConfig.setConsumerSecret(anonymous);
    ...


  • 如果您使用的是需要OAuth 2.0身份验证的外部(非Google)服务,请按照迁移指南进行操作。

    $ b $



    是的,即使在图书馆中,它也比OAuth1更复杂 - 但必然如此。


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

    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?

    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?

    解决方案

    You'll find a function that generates and saves PDFs for one or all of your sheets in Convert all sheets to PDF with Google Apps Script.

    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.

    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.

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

    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.

    To convert your script:

    1. Replace

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

      with

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

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

    That's all there is to it.

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

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

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

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