I am currenthy having trouble with creating an apps script function that queries bigquery when trying to use it in sheets. Essentially I have written a function in appscript that executes a
BigQuery.Jobs.query
and outputs a single value e.g. the sum of all queried data. When I run this function directly in apps script it works just fine with the correct result when checking with Logger.
However, when putting the function in a cell in my google sheet in order to have the returned value in the cell I get the following error instead:
GoogleJsonResponseException: API call to bigquery.jobs.query 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 10).
The link did not really help me, but it seems like an authentication issue.
It sounds like youâre facing an OAuth 2.0 authentication problem when using a Google Apps Script function that queries BigQuery from within Google Sheets. The error message indicates improper authentication when called from Sheets, even though it works in the Apps Script editor.
If youâre using the OAuth2 library, ensure itâs set up correctly. However, BigQuery has a built-in service, so you might not need it. Use the built-in Google authentication instead.
3. Check Project Credentials:
Ensure your Apps Script projectâs linked Google Cloud Project has the BigQuery API enabled.
Verify credentials are properly set up in the Google Cloud Console.
4. Token Usage:
If using ScriptApp.getOAuthToken(), ensure itâs passed correctly in the BigQuery API request header.
However, with built-in BigQuery service, manual token handling shouldnât be needed.
5. Deployment and Permissions:
The script should prompt for authorization the first time you run it from Sheets.
If scopes changed after deployment, reauthorize.
6. Testing and Debugging:
Test the script in the Apps Script environment for expected behavior, especially when called from Sheets.
Use the Logger or new IDE debugging features.
7. Refresh the Sheet:
After script changes, refresh the Sheet to ensure it uses the latest version.
8. Review Execution Logs:
Check the Apps Script dashboardâs execution logs for additional error details when running the script from Sheets.
These are great instructions, on note to add to @ms4446 very clear trouble shooting tips is regarding
A problem Iâve seen in the past that returns the same error is in you GCP you need the âBigQuery Job Userâ role added to the account running the script
Youâre absolutely right. Ensuring that the account running the script has the appropriate roles in Google Cloud Platform (GCP) is a crucial step. The âBigQuery Job Userâ role is necessary for executing jobs in BigQuery, including queries, load jobs, export jobs, etc.
but it doesnât work in sheets in both cases. I do get the Authentication request in appscript on my first run, but in sheets no pop-up ever appears.
3.I have the BigQuery API enabled in the Apps script project. I have since added the âBigQuery User Roleâ to my account. I am now Owner, BigQuery admin and BigQuery User.
I think the AuthToken line doesntâ work in Sheets for me anyay. If I add the code
this line gets skipped in AppsScript. The cell will however return â100â in the cell in Google Sheets.
Authorization popped up for me in Apps Script but never in Sheets. Is there any way to force it to?
It works just fine in Apsp Script. The query gets executed even without the AuthToken-line. I suspect this is because I actually get a pop-up for authorization and can authorize the script acoordingly in this environment.
I tried this after every change. Nothing seemed to work.
The execution log in Apps Script shows no errors. Itâs only when the function is called in sheets.
Ensure that your script is deployed as an API executable. This is different from a standard script deployment and is necessary for it to be called from external sources like Google Sheets.
Go to the Apps Script Editor, click on âDeployâ > âNew deploymentâ. Choose âAPI executableâ as the deployment type. This might help with the authentication issue when the script is called from Sheets.
Explicit Authorization Flow:
Sometimes, the authorization flow does not trigger automatically in Google Sheets. You can try creating a separate function in your Apps Script that explicitly calls for authorization.
This function can be a simple script that requires the same scopes as your main function. Run this function directly from the Apps Script editor to trigger the authorization flow.
Spreadsheet Bound Script:
If your script is not bound to the specific Google Sheets file, consider making it a bound script. This can sometimes resolve issues with permissions and authentication.
To create a bound script, open your Google Sheets file, go to Extensions > Apps Script, and paste your script there.
Check for Conflicting Authorizations:
If you have multiple Google accounts or if your script was previously authorized with different credentials, this might cause conflicts. Try clearing your browserâs cache and cookies, or use an incognito window to eliminate this issue.
Manual Token Handling:
If youâre manually handling the OAuth token in your script, ensure that itâs being used correctly in the BigQuery API request. However, this shouldnât be necessary if youâre using the built-in BigQuery service in Apps Script.
Review Project Settings:
Double-check that the Google Cloud Project linked to your Apps Script has the correct settings and that the BigQuery API is enabled.
Ensure that the project number in the Google Cloud Console matches the project number in the Apps Script project settings.
Logging and Debugging:
Enhance logging in your script to capture more details, especially when the function is called from Sheets. This might provide more insights into where the process is failing.
I tried this and the process worked. I still get the same error in sheets however.
I wrote a function that removes authorization using
ScriptApp.invalidateAuth(); and wrote another one that calls for authorization using an UI-element. Using this technique I can now force authorization in Google Sheets directly and it does display the correct authorization scopes (see image)
Accepting still makes it so the script runs fine in the Apps Script Editor but gives the same authorization error when trying to run it in Sheets.
[quote=âms4446â] Spreadsheet Bound Script:
If your script is not bound to the specific Google Sheets file, consider making it a bound script. This can sometimes resolve issues with permissions and authentication.
To create a bound script, open your Google Sheets file, go to Extensions > Apps Script, and paste your script there.
[/quote]The Apps Script was created as a bound script originally.
I have since removed the manual token handling. When using the OAuth2 Library command
ScriptApp.getOAuthToken()
doesnât generate a token when the script is run through sheets and only in Apps Script (I checked with Logger in Apps Script and return in Sheets).
The project numbers match up (I can even click the number in AppScript which leads me to my GCP Panel). The Bigquery API is also enabled and shows 0 Errors.
You mentioned you added other BigQuery roles to your project but not âBigQuery Job Userâ - my suggestion would be to test if adding this role resolves the issue
I have the roles âBigQuery Adminâ, âBigQuery Job Userâ and âOwnerâ, but it does not work. I have had these roles since before my previousr reply.