Open wiktor-cobry opened 3 years ago
Here's a suggestion as to what that function could look like
/**
* Returns true if the token is accepted by the API.
* Returns false if the API responds with a 401 status code (i.e. it was rejected)
* @return {boolean} True if the token is accepted by the API.
*/
function isTokenValid() {
var token = getOauthService().getAccessToken();
var apiEndpoint = "your endpoint here";
var apiResponse = UrlFetchApp.fetch(`${apiEndpoint}`, {
headers: {
'Authorization': `Bearer ${token}`
},
muteHttpExceptions: true
});
var jsonResponse = JSON.parse(apiResponse);
if (jsonResponse.status == "401") {
return false;
}
else {
return true;
}
}
It's this method in question.
Expected Behavior
The code sample should either:
Description: This code sample suggests that calling the
hasAccess()
method guarantees authorisation. This isn't true.Actual Behavior
The response code needs to be handled to fully confirm authorisation state, or the documentation should suggest that the hasAccess() method doesn't guarantee authorisation. Similar to the way the call is handled in the Google Workspace Addons Documentation