I am using the following code to get version information on my script file:
function getProjectVersionNumber(scriptId, theAccessTkn) {
try {
var allVersions, errMsg, highestVersion, options, payload, response, url;
if (!scriptId) {
//Logger.log('There was an error - No scriptID')
//Error handling function
throw new Error('There is no script ID for fnk getProjectVersionNumber');
}
if (!theAccessTkn) {
theAccessTkn = ScriptApp.getOAuthToken();
}
url = "https://script.googleapis.com/v1/projects/" + scriptId + "/versions";
options = {
"method": "GET",
"muteHttpExceptions": true,
"headers": {
'Authorization': 'Bearer ' + theAccessTkn
}
};
response = UrlFetchApp.fetch(url, options);
//Logger.log('response : ' + JSON.stringify(response).slice(0,500));
let rc = response.getResponseCode();
let rccontext = response.getContentText();
Logger.log("Response code %s, context returned %s",rc,rccontext);
let responseJ = JSON.parse(response);//The response must be parsed into JSON even though it is an object
if (typeof responseJ === 'object') {
errMsg = responseJ.error;
if (errMsg) {
errMsg = errMsg.message;
Logger.log(errMsg);
return 'err' + errMsg;
}
}
allVersions = responseJ.versions;//Get versions
highestVersion = allVersions[0].versionNumber;
//Logger.log("highestVersion: " + highestVersion)
return highestVersion;
} catch (e) {
myErrorHandlingFunction_(e);
}
}
function go () {
var scriptID = ScriptApp.getScriptId();
var theAccessTkn = ScriptApp.getOAuthToken();
let l = getProjectVersionNumber(scriptID,theAccessTkn);
Logger.log(l)
}
when I execute this, I get a 403 permission denied error:
Response code 403.0, context returned {
"error": {
"code": 403,
"message": "Request had insufficient authentication scopes.",
"status": "PERMISSION_DENIED",
"details": [
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
"reason": "ACCESS_TOKEN_SCOPE_INSUFFICIENT",
"domain": "googleapis.com",
"metadata": {
"method": "google.apps.script.management.v1.ProjectsService.ListVersions",
"service": "script.googleapis.com"
}
}
]
}
}
In google console, I have verified that I have the Apps Script API enabled.
When I use the Google Workplace script playgound to execute this api, it works fine me. https://developers.google.com/apps-script/api/reference/rest/v1/projects.versions/list
Why won't this work in the script editor?
Most of the time when I get insufficient scope errors it tells me what I have to add and I add it and move on.
I modified your code down to the essentials and it ran ass follows:
gobj.globals.scriptid is my global variable for my script id
I think one of these is the scope:
"https://www.googleapis.com/auth/script.scriptapp"
,"https://www.googleapis.com/auth/script.deployments"