You do not have permission to call "UrlFetchApp.fetch"

Viewed 10448

I am sending http request from script editor in google spreadsheets, but I keep getting the following error message:

`Google Apps Script: You do not have permission to call UrlFetchApp.fetch. Required permissions: https://www.googleapis.com/auth/script.external_request`

I am using onEdit function:

 function onEdit(e){
  var ui = SpreadsheetApp.getUi();
        var response = UrlFetchApp.fetch('http://www.eur-api.idomoo.com/');
    Logger.log(response.getContentText());
  } 

I don't know Why am I getting this error? and I also gave permission to script.external_request scope, Any help would be appreciated.

6 Answers

onEdit is invoked by a Simple Trigger when a user changes a value in a spreadsheet.

However, simple triggers cannot access services that require authorization, such as UrlFetchApp.fetch. See the Google Apps Script guide

What you can do is to simply rename the function onEdit to something else, such as atEdit, so as to remove the simple trigger. Then follow the Current project's triggers menu...

enter image description here

and add a trigger to be called in the event of On edit.

And when creating the trigger, you will follow the Wizard to grant your apps script permission to connect to an external service.

There are two ways to solve this

I. Update your manifest and add the line "https://www.googleapis.com/auth/script.external_request" to oauthScopes

{
  "timeZone": "Europe/Moscow",
  "oauthScopes": [
    ...
    "https://www.googleapis.com/auth/script.external_request"
  ],
  "dependencies": {
    ...
  },
  "exceptionLogging": "STACKDRIVER"
} 

II. Or remove oauthScopes key from your manifest totally

You can find how to edit the manifest here Manifests

I know its an old thread, but I find it weird of having to rename the method to something else, e.g. from onEdit to onEditHandler just to make it work. It turns out that I can make it work by:

  1. Remove the trigger.
  2. Re-add the trigger.

This is possible probably due to previously the handler doesn't have the fetch url, therefore it doesn't have to ask for authorization to access external request. Once it is re-added, then it has the proper authorization because you are asked to re-authorize the handler.

I experienced this error after adding the onEdit function without also explicitly adding a trigger. This was resolved by adding an onEdit trigger that calls the function. Triggers -> Add Trigger, On edit event type.

Related