1
votes

I am trying to create a custom Google Sheets function that pulls data from the Analytics Reporting V4 API. When I run this function from the App Scripts console, it successfully returns data.

When I run the function from within my spreadsheet, I get the following error:

API call to analyticsreporting.reports.batchGet failed with error: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. (line 59).

Here is my function defined in the Apps Script Editor (again, this works fine when I press the run button from here):

code editor screenshot

And here is my error-producing spreadsheet implementation:

formula edited in a cell screenshot

error screenshot

Is there a way that I can make this work without having to use OAuth credentials? From my understanding, the benefit of using Advanced Google Services is the ability to avoid this authentication flow, and I would like to take advantage of it.

1
Custom functions runs anonymously. So, you can't do that(at least not without weakening your Google account security) - TheMaster
What is the preferred way to handle this then? - Zach Gollwitzer
You can substitute the custom function with any of the following: button, time-trigger, installed triggers based on edit or on open. - TheMaster

1 Answers

3
votes

In the documentation:

If your custom function throws the error message You do not have permission to call X service., the service requires user authorization and thus cannot be used in a custom function. To use a service other than those listed above, create a custom menu that runs an Apps Script function instead of writing a custom function. A function that is triggered from a menu will ask the user for authorization if necessary and can consequently use all Apps Script services.

Using Apps Script Services