Script suddenly getting permission denied

154 views
Skip to first unread message

BTV Analytics

unread,
May 23, 2024, 8:35:43 PM5/23/24
to Google Ads Scripts Forum
Hi, my daily scheduled script is suddenly getting the following error: 

5/23/2024 11:08:03 AM

Exception: You do not have permission to access the requested document. at main (Code:9:36) at Object.<anonymous> (adsapp_compiled:20513:54)
The script has worked for months up until this point. below is the script. Would you be able to offer any insight as to what might be happening?

function main() {
  // Define the custom date range
  var today = new Date();
  var firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
  var startDate = Utilities.formatDate(firstDayOfMonth, 'GMT', 'yyyyMMdd'); // first day of current month
  var endDate = Utilities.formatDate(new Date(), 'GMT', 'yyyyMMdd'); // today's date
 
  // Open the spreadsheet
  var spreadsheet = SpreadsheetApp.openById('XXXXXXXXXXXXXXXXXXX'); // Worksheet Id
  var sheet = spreadsheet.getSheetByName('DL_PMAX'); // Sheet name
 
  // Clear the existing content
  sheet.clearContents();
 
  // Set the headers
  sheet.appendRow(['Account Name','Account ID','Campaign Name', 'Campaign ID', 'IsPaused?', 'Budget Amount', 'Cost','Monthly Budget', 'Cost/Conv']);
 
  // Select the account
  var mccAccount = AdsApp.currentAccount();
  var accountIterator = AdsManagerApp.accounts().get();
  while (accountIterator.hasNext()) {
    var account = accountIterator.next();
   
    // Select the client account to operate on.
    AdsManagerApp.select(account);
 
  // Get all PMAX campaigns
    var performanceMaxCampaignIterator = AdsApp.performanceMaxCampaigns()
      //.withCondition('Status = ENABLED') // only include running campaigns
      .get();
    while (performanceMaxCampaignIterator.hasNext()) {
      var campaign = performanceMaxCampaignIterator.next();
     
     
      // Get the budget
      var budget = campaign.getBudget();
      var budgetAmount = budget.getAmount();
      var monthlyBudget = budgetAmount * 30.4; // assuming 30.4 days in a month
     
      // Get the cost for the custom date range
      var report = AdsApp.report(
        'SELECT CampaignName, CampaignId, Amount, Cost, CostPerConversion ' +
        'FROM CAMPAIGN_PERFORMANCE_REPORT ' +
        'WHERE CampaignId = ' + campaign.getId() +
        ' DURING ' + startDate + ',' + endDate
      );
     
      // Add the budget information to the sheet
      var rows = report.rows();
      while (rows.hasNext()) {
        var row = rows.next();
        sheet.appendRow([account.getName(), account.getCustomerId(), row['CampaignName'], campaign.getId(), campaign.isPaused(), budgetAmount, row['Cost'], monthlyBudget, row['CostPerConversion']]);
      }
    }
   
    // Select the MCC account again
    AdsManagerApp.select(mccAccount);
  }
}

Nils Rooijmans

unread,
May 24, 2024, 2:51:18 AM5/24/24
to Google Ads Scripts Forum

This error typically occurs when the email address of the account that you’re using to authorize and execute the Google Ads Script does not have write/editor access to the used spreadsheet (or another Google Drive document) in your script.

Solution:

Use the same email address to authorize and run the Google Ads Script that also has write/editor access to the used spreadsheet/document. 

See more details ->  https://nilsrooijmans.com/google-ads-scripts-faq/exception-you-do-not-have-permission-to-access-the-requested-document/

Hope this helps,


Nils Rooijmans
https://nilsrooijmans.com
See my Google Ads Scripts FAQ to avoid the same mistakes I made: https://nilsrooijmans.com/google-ads-scripts-faq/

Google Ads Scripts Forum Advisor

unread,
May 24, 2024, 4:55:37 AM5/24/24
to adwords...@googlegroups.com
Hi,

Thank you for reaching out to the Google Ads Scripts support team!

I would like to inform you that the error message “Exception: You do not have permission to access the requested document” indicates that you do not have access to the spreadsheet URL in the script. You can ask the owner of the script to provide access to the spreadsheet that is used in the script. Please follow this guide to share a file publicly. I suggest you refer to the document SpreadsheetApp to get more information.
 
I hope this helps! Feel free to get back to us if you still face any issues.
 
This message is in relation to case "ref:!00D1U01174p.!5004Q02tJ581:ref" (ADR-00236327)

Thanks,
 
Google Logo Google Ads Scripts Team


 

Reply all
Reply to author
Forward
0 new messages