You do not have permission to call "UrlFetchApp.fetch"
Asked Answered
C

7

38

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.

Cheesy answered 12/10, 2019 at 23:25 Comment(0)
S
56

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.

Segal answered 8/7, 2020 at 9:36 Comment(10)
Nice solution, Yuci!Kezer
This menu does not exist in my app script project, this seems outdatedStative
@DimitriKopriwa The project triggers are available from the left side bar.Misshape
Thanks! Hard to believe that such a simple workaround exists!! :DHerminahermine
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!Lamoree
@Lamoree I am getting the same error related to onOpen() , how can I eliminate it? Could you describe a bit more please?Slider
@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.Lamoree
@Lamoree , 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.Slider
@HarshJ At this point, I think you need to make this a question.Lamoree
"you will follow the Wizard to grant your apps script permission" this part did not happen for me; permissions are still failing.Tilbury
F
12

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

Fluorspar answered 13/10, 2019 at 5:55 Comment(3)
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.Prokofiev
@HesamJavadi, yep. It's that what you can find if you'll follow to my link in post. Cheers!Fluorspar
You might have to reload the GSheet to reregister the appscript.json oauthScopes value in the project before it's applied to calls from the project.Vrablik
E
5

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.

Enculturation answered 18/11, 2020 at 7:56 Comment(0)
T
3

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.

Thomasinethomason answered 15/5, 2022 at 17:45 Comment(0)
E
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

Entoderm answered 13/10, 2019 at 0:9 Comment(4)
I got the popup to confirm. Confirmed and it still didn't run after this.Byelection
Are you the owner of the document? @Byelection Another issue might be if you're operating out of a GSuite account, instead of a root gmail account.Entoderm
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.Byelection
@Byelection 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.Entoderm
E
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

Extravert answered 13/10, 2019 at 0:16 Comment(0)
E
0

You should

Remove Current triggers 

and then

create new trigger by selecting event "OnEdit"
Extramarital answered 4/11, 2022 at 7:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.