Google App Script: access denied when trying to create a folder
<p>I check my Gmail account. If there are new messages with image attachments, I create a subfolder into my Google Drive account, with folder name as email subject, and create a file for every single attachment.</p>
<p>Every thing works well when trying to create subfolder in the root, using <code>DriveApp.createFolder()</code>, but I got an error when using <code>.createFolder()</code> method of a <code>Folder</code> object.</p>
<blockquote>
<p>This is the <a href="https://developers.google.com/apps-script/reference/drive/folder#createfoldername" rel="nofollow noreferrer">link to App script documentation</a> that prove that we can create a folder using a Folder object.</p>
</blockquote>
<p>This is the code:</p>
<pre><code>var folders = DriveApp.getFoldersByName(&quot;Received photos&quot;);
if (!(folders.hasNext())) {
  Logger.log (&quot;Recevied photos folder not found!&quot;);  
} else {
  var main_folder = folders.next();
  Logger.log (&quot;Going into &quot; + main_folder.getName());
  var main_folder_id = main_folder.getId();
  
  
  // Move every attachment to right folder
  var folders = main_folder.getFoldersByName(title);
  if (folders.hasNext()) {
    Logger.log (&quot;Folder exists&quot; + title);  
    folder = folders.next();
  } else {
    Logger.log (&quot;Folder &quot; + title + &quot; not exists, I create it&quot;);  
    /**********************************************************
    *       THE FOLLOWING ROW GIVE ME ACCESS DENIED ERROR
    ***********************************************************/
    folder = main_folder.createFolder(title);
  }
  
  try {
    attachments.forEach(function (attachment, idx_att)  {
      var moved_file = folder.createFile(attachment);
    });
  } catch (e) {
    Logger.log(e.toString());
  }

}
</code></pre>
<p>I got this error</p>
<blockquote>
<p>Access denied: DriveApp. (riga 57, file &quot;Production&quot;)</p>
</blockquote>
<p>at this row</p>
<pre><code>folder = main_folder.createFolder(title);
</code></pre>
<p>I checked my logs so I'm sure that <code>main_folder</code> is an existing folder.</p>
<p>It's a my folder, it's not shared from someone to me. So, why App script can't create a subfolder into it?</p>
<p>EDIT:</p>
<p>I tried to debug my permission adding these lines</p>
<pre><code>Logger.log(Session.getActiveUser().getEmail());
var access = main_folder.getAccess(Session.getActiveUser());
Logger.log(access);
</code></pre>
<p>The users email is right, it's the owner. But the second log line tell me <code>VIEW</code>. Now the question is <strong>why can't I have write access yo my folders using this App Script standalone script?</strong></p>
<p>EDIT 2:</p>
<p>I tried to verify ownership</p>
<pre><code>var owner = main_folder.getOwner();
Logger.log(owner.getEmail());
</code></pre>
<p>What a suprise! The owner email isn't my email but it's the email of the user who shard with me this script... but I verified on Drive and the REAL owner of the main folder is me...</p>
<p><strong>So what am I doing wrong and how to be able to use a shared script to write into a private folder?</strong></p>