Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 210
Haq Nawaz Avatar asked Oct 12 '19 23:10

Haq Nawaz


People also ask

What is UrlFetchApp Fetch()?

UrlFetchApp. Fetch resources and communicate with other hosts over the Internet. This service allows scripts to communicate with other applications or access other resources on the web by fetching URLs. A script can use the URL Fetch service to issue HTTP and HTTPS requests and receive responses.

Is UrlFetchApp synchronous?

UrlFetchApp methods are synchronous. They return an HttpResponse object, and they do not take a callback.


Video Answer


2 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.

like image 193
Yuci Avatar answered Sep 21 '22 15:09

Yuci


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.

like image 25
O.O Avatar answered Sep 17 '22 15:09

O.O