I have a Google sheets add-on listed on the market. In most cases it works OK, but for something like 10% of the users I can see in the Log Explorer the following security Notice level entry:
{
"insertId": "-95cptdf17zlkw",
"jsonPayload": {
"serviceContext": {
"service": "AKfycbwdlQO2SpUoJE-rL-QMnni4dihrhsvrx16GwESJswoy8NA"
},
"message": "Authorisation is required to perform that action."
},
"resource": {
"type": "app_script_function",
"labels": {
"project_id": "formula-tracer-side-bar",
"invocation_type": "unknown",
"function_name": "excuteUserRequest"
}
},
"timestamp": "2022-05-17T15:56:10.170Z",
"severity": "NOTICE",
"labels": {
"script.googleapis.com/process_id": "EAEA1GOyaeEOdACtJlc76JEW70I8J5yujif1YQ8poDnneE6Tlg85jwUqn_oSIWZygqSxQ6s6DPtMt4jkIVo6Ak-2W8lR0K6Eyldw8kTvxvn-dauNu4PbQ8cgbpvGcReRquwiaOeV-ZehZev1w-1gmujKdpnPk95NERrAmskkifGIhFhjfmKrcb_wIdJENMD_ww4kgZWxiMJ0VSmNsV-Qr2d_FFoek6tF09xXJMgtIHBVWLEG9aJrn7GLfOfebJqj4L_RXWu1xu4aSKFiayU-HcJH_8-Yuo8FSMyIeC_Qd",
"script.googleapis.com/project_key": "Mzy7e87MSzO0enTUViEAdROLp-GB2FQma",
"script.googleapis.com/deployment_id": "AKfycbwdlQO2SpUoJE-rL-QMnni4dihrhsvrx16GwESJswoy8NA"
},
"logName": "projects/formula-tracer-side-bar/logs/script.googleapis.com%2Fconsole_logs",
"receiveTimestamp": "2022-05-17T15:56:10.579606416Z"
}
Relevant add-on pseudo code:
Server
function onInstall() {
onOpen();
}
//add menu items to spreadsheet
function onOpen() {
let menu = SpreadsheetApp.getUi().createAddonMenu();
menu.addItem('Show side bar', 'showSidebar');
menu.addToUi();
}
// called by menu items
function showSidebar() {
let template = HtmlService
.createTemplateFromFile("sideBar");
let htmlOutput = template.evaluate();
htmlOutput.setTitle(getHTMLTitle());
SpreadsheetApp.getUi().showSidebar(htmlOutput);
}
//called by client
function excuteUserRequest(param){
console.info("excuteUserRequest",param);
//handle client request
processRequest()
}
Client
//client
window.onload = function(){
/* do some html initializtions*/
initPage();
//call server when addon is launched
sendRequest();
}
function sendRequest(){
let param = getParam();
//Call trace on server
google.script.run
.withSuccessHandler(onSuccessRequest)
.withFailureHandler(onFailueRequest)
.excuteUserRequest(param);
}
The server call to
excuteUserRequestin the clientsendRequest()is missing permissions. Looks like that the user(s) is not recognized by google as the error entry (although is logger entry isNoticelevel) is missing theuser_keyinformation as in most log entriesAdd-on is listed and approved after it successfully passed the oAuth verification process. So as I can understand it - the user approved the required permissions the add-on asks while installing
I can not reproduce this situation. I thought it might be something with the user permissions for the specific spreadsheet the user is using. However, when using spreadsheets with no edit permission, the Google Sheets extensions menu is disabled thus the addon can not be launched at all.
this post looks similar (client call app script server function sometimes fails with missing authorization), but I do not really understand if it is actually triggered by the same cause and if so, what to do.
Other posts (this one and this one) looks irrelevant as they refer executing app script function - not one called from client, suggesting to manually run the function in the script editor to get the relevant permissions.
some clarification:
user_keylike actually all log fields accept thejsonPayload.messageare added to the log entry. for example - the following log entry is from withinshowSidebar()the one before the error message.
the log message is only "X.XXX@YYYY.com showSideBar count: 0"
where:
X.XXX@YYYY.com is the user email added by the addon for any console.log().
all other log fields are added by google for each console.log(msg).
this includes the user_key which is assigned by Google.
To shortly summarize:
So same user first call showSideBar() from the spreadsheet extensions menu. This works fine, but the second call triggered immediately fails.
{
"insertId": "bxz0h0f1zmrbn",
"jsonPayload": {
"serviceContext": {
"service": "XXX",
"version": "27"
},
"message": "X.XXX@YYYY.com showSideBar count: 0"
},
"resource": {
"type": "app_script_function",
"labels": {
"function_name": "",
"invocation_type": "menu",
"project_id": "formula-tracer-side-bar"
}
},
"timestamp": "2022-05-17T15:56:12.864Z",
"severity": "INFO",
"labels": {
"script.googleapis.com/project_key": "AAA",
"script.googleapis.com/process_id": "BBB",
"script.googleapis.com/deployment_id": "CCC",
"script.googleapis.com/user_key": "DDD"
},
"logName": "EEE",
"receiveTimestamp": "2022-05-17T15:56:13.653216430Z"
}
Please advise
