-1

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:

  • Checked the scopes in the project manifest file (appsscript.json) to ensure both Drive and Gmail APIs are included.
  • Verified that I’ve authorized the script fully for both services by login in prompt window and allow permission.
  • Tried separating the calls into different functions, but the issue persists when combined. What could be causing this, and how can I resolve it to use both services in the same script?
  • I have tried to create new project and write out the code from scratch but no luck
  • clear out cache memory login through different browser etc
6
  • I was trying to replicate your issue, and your issue might be coming from the consent screen that should pop up when running. Since multiple services are being called, there should be a checkbox that would show on the side of each scope and it needs to be checked. I do think that you missed this during the authentication process, can you confirm this? Commented Dec 19, 2024 at 12:55
  • @Alma_Matters yes I checked all those checkboxes and still it's now working however when i using either drive or either gmail service it's working Commented Dec 19, 2024 at 13:54
  • I recall that two scopes you mention, when present together in a script project, result in stricter verification requirements. See the links at OAuth App Verification Help Center for more info. Commented Dec 19, 2024 at 14:22
  • @Alma_Matters the link you provided is totally different approach and issue what i facing the folder is in my drive i am the owner of the folder and i am accessing my Gmail also using my google apps script code and initially I logged through Gmail prompt and provide all permission it was earlier working without issue I am facing this since yesterday when trigger stopped working and now even over manually run function it's not working Commented Dec 19, 2024 at 16:08
  • Your code is working fine for me. Please ensure that the FolderId and sheetId belong to a folder and spreadsheet stored in My Drive (the root folder for the account running the script). Commented Dec 27, 2024 at 21:27

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

4 Comments

I am not saying it's not working that's the weird thing i facing code is correct and when i try with another email this code working there but earlier it was working in my business account also for some automation. however it stopped suddenly and Now i am getting this error
If the issue happens only with one account, consider asking: Is this the full code, or is there additional code running in this file? Could you provide more details about the file? Is it associated with a Gmail account or a business account with different administrative settings? @codingWars
@Alma_Matters I am checking this with business account and yes I can tell whole code logic where I found issue is only this part which I put in question where i call both service together i am having issue even my triggers are not firing too but that's diffrent issue but over single service call or call with spreadsheetApp and DocumentApp having no isseu. very strange error to me
@codingWars It looks like the question body is incomplete. Please edit the question to add all the relevant details directly in the question body.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.