33

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.

user1338062
  • 11,939
  • 3
  • 73
  • 67
Haq Nawaz
  • 412
  • 1
  • 4
  • 12

7 Answers7

49

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.

Yuci
  • 27,235
  • 10
  • 114
  • 113
  • 1
    Nice solution, Yuci! – radical7 Sep 23 '20 at 17:40
  • This menu does not exist in my app script project, this seems outdated – Dimitri Kopriwa Dec 01 '21 at 12:44
  • @DimitriKopriwa The project triggers are available from the left side bar. – vstepaniuk Dec 17 '21 at 08:56
  • Thanks! Hard to believe that such a simple workaround exists!! :D – ankush981 Dec 30 '21 at 02:26
  • 1
    Using the legacy editor, I didn't even have to change the name of my onOpen() method. There were no triggers specified, so I added one for onOpen() and it worked. Thanks! – Marvo Feb 08 '22 at 05:38
  • @Marvo I am getting the same error related to onOpen() , how can I eliminate it? Could you describe a bit more please? – Harsh J Jul 25 '22 at 17:49
  • @HarshJ, from your sheet, menu "Extentions->App Script". From there, "Edit->My Current Triggers". Should be in the editor now. Choose Triggers from the left side. Add a trigger (blue button bottom right.) Add an "onOpen" trigger, and select your function. Why this is necessary, I dunno. If you share the script, this trigger is NOT shared with it. – Marvo Jul 26 '22 at 19:39
  • @Marvo , So won't it work if I am making an add-on for google marketplace? Taking about the trigger thing as you mentioned the info related to it of not being able to be SHARED. – Harsh J Jul 27 '22 at 15:55
  • 1
    @HarshJ At this point, I think you need to make this a question. – Marvo Jul 28 '22 at 21:06
10

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

evandrix
  • 6,041
  • 4
  • 27
  • 38
contributorpw
  • 4,739
  • 5
  • 27
  • 50
  • To view the manifest in your Google editor, follow these simple steps: 1- Open your Google Apps Script project. 2- Navigate to "Project settings" in the app script, located in the left-side menu. 3- Check "Show 'appsscript.json' manifest file in editor." By following these steps, you'll be able to access and modify the manifest file with ease. – Hesam Javadi Jul 24 '23 at 09:09
  • @HesamJavadi, yep. It's that what you can find if you'll follow to my link in post. Cheers! – contributorpw Jul 24 '23 at 14:12
4

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.

O.O
  • 7,179
  • 1
  • 34
  • 32
1

You'll need to authorize your script to access the external_request service. If you're the owner of the project, you should be able to grant access by running and confirming with the oauth page.

Read more about authentification here: https://developers.google.com/apps-script/guides/services/authorization

Yaakov Bressler
  • 9,056
  • 2
  • 45
  • 69
  • I got the popup to confirm. Confirmed and it still didn't run after this. – JGood Oct 30 '20 at 22:27
  • 1
    Are you the owner of the document? @JGood Another issue might be if you're operating out of a GSuite account, instead of a root gmail account. – Yaakov Bressler Oct 30 '20 at 23:39
  • 1
    Thanks for the reply, Yaakov! I was operating out of my GSuite account, but am the owner of the domain and created the spreadsheet. Can you send reference to that being an issue? I was able to get the answer from Yuci to work, but would prefer to have a more global approval like your answer. For reference, it popped up the oauth request, but then continued to throw an exception. – JGood Nov 03 '20 at 22:46
  • 1
    @JGood this is one of those nitty gritty cases where documentation is sparse. I know GSuite to be problematic from heuristic. I'm certain the documentation exists somewhere, but not someplace straightforward. I'd be thrilled to be proven wrong on this. – Yaakov Bressler Nov 03 '20 at 23:10
1

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.

user2386601
  • 101
  • 6
0

Try changing the link https.

Looking at their api documentation:

API Endpoints Idomoo’s API 2.0 has several endpoints all starting from one of several roots, depending on the territory in which you want your data sent to and processed:

USA https://usa-api.idomoo.com/api/v2 EU https://eur-api.idomoo.com/api/v2

Mark B
  • 415
  • 4
  • 13
0

You should

Remove Current triggers 

and then

create new trigger by selecting event "OnEdit"
Adriaan
  • 17,741
  • 7
  • 42
  • 75