Search code examples
google-apps-scriptweb-applicationsgoogle-drive-api

Getting the "You do not have permission to call getFolderByID" error, when calling that method in HTML


I am going through the book Google Apps Scripts, 2nd Edition. The lesson I am on for creating a web app asked me to use the following code:

function doGet() {
var html = HtmlService.createTemplateFromFile('index').evaluate()
.setTitle('06 Automating Forms')
.setSandboxMode(HtmlService.SandboxMode.NATIVE);
return html;
}

However, when I run this code I get the "You do not have permission to call getFolderById" error message. I don't understand why I am getting this error message. Also, it says the error is on line 2.

I am calling the "getFolderByID" method in my index.html file. Here is that code:

<div class="body">
<div><h2>This App will allow you to create a form from a template
in Google Docs.</h2></div>
<hr>

<div id="options">
  <?var files = 
DriveApp.getFolderById('0B6YzuBeNxooXSWE3Vm9WMlVnWkk').getFiles();?>
<select id='template'>
  <option value="notSel">Select a Template</option>
  <?while (files.hasNext()){
  var file = files.next();?>
  <option value="<?=file.getId()?>"><?=file.getName()?></option>
  <?}?>
 </select>
 </div>

</div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
</script>

 <style>
 .body{
 padding: 10px;
 }
 </style>

Is there a way to get permission to call that method? I thought if I run the code a box pops up and allowing me access to that area of Drive.


Solution

  • Only script files have the ability to ask for authorization, html files don't. The simplest thing to do is to add a dummy function in your code.gs file like this :

    function myFunction() {
      DriveApp.getFolderById('0B6YzuBeNxooXSWE3Vm9WMlVnWkk');
    }
    

    run it from the script editor and you will get the authorization request as expected.