codingWars
codingWars

Reputation: 9

DriveApp and GmailApp Services not working simultaneously in Google Apps Script

I'm encountering an issue with Google Apps Script when trying to use DriveApp and GmailApp together. Here's the situation:

  1. When I call DriveApp alone, it accesses folders without any issues.
  2. Similarly, when I call GmailApp alone, it works perfectly and retrieves emails as expected.
  3. However, when I try to use both DriveApp and GmailApp in the same script, I get the
    following error: "Exception: Permission required to access."

Code.gs

function test () {
    let folder = DriveApp.getFolderById(FolderID);
    let threads = GmailApp.search('is:starred')
    let sheet = SpreadsheetApp.openById(sheetID)

    console.log({
        folder : folder,
        threads : threads,
        sheet : sheet
    })
}

When these two services are called together in the same script, it throws the error.

error: "Exception: Authorization is required to perfom that action test @code.gs.2"

Troubleshooting steps I’ve tried:

Upvotes: -1

Views: 83

Answers (1)

Cooper
Cooper

Reputation: 64032

It runs okay for me:

function test () {
    let folder = DriveApp.getFolderById(gobj.globals.testfolderid);
    let threads = GmailApp.search('is:starred')
    let sheet = SpreadsheetApp.openById(gobj.globals.test1id)

    Logger.log(JSON.stringify({
        folder : folder.getName(),
        threads : threads,
        sheet : sheet.getName()
    }))
}

Execution log
9:42:35 AM  Notice  Execution started
9:42:35 AM  Info    {"folder":"Testfolder","threads":[{},{},{},{},{}],"sheet":"Test1"}
9:42:37 AM  Notice  Execution completed

Upvotes: 0

Related Questions