18
votes

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.

5

5 Answers

20
votes

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.

3
votes

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

2
votes

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

1
votes

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.

0
votes

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