0
  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?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
jason
  • 3,811
  • 18
  • 92
  • 147

2 Answers2

4

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.

Community
  • 1
  • 1
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • Sweet. I was just about to say... I'm not very knowledgeable on systems design, but why did they change something so simple to something so complicated even when I'm not going outside their environment. I'm in their app and signed in, what more authentication do they need? Didn't make sense to me. I'm glad there is a better way. – jason Jun 02 '15 at 20:19
  • btw, you are missing a `,` before `"muteHttpExceptions"` – jason Jun 02 '15 at 20:39
3

Here is the change. Because you are using DriveApp your script already has the authorization to access your files from any source including UrlFetchApp. All you have to do is get the token from the script and pass it in the header of your Fetch request.

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)
}
Spencer Easton
  • 5,642
  • 1
  • 16
  • 25