Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
992 views
in Technique[技术] by (71.8m points)

google apps script - Printing spreadsheet to PDF then saving file in Drive using 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)
}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...