This .gs code checks if the document is authorized, and if so runs a post request. I am trying to only have to provide OAuth once rather than every time the token expires, any advice is appreciated!
function Gmail() {
if (service.hasAccess()) {
var toolKeyFinal = service.getAccessToken();
//*******request would run with token here********
}
else{
var authorizationUrl = service.getAuthorizationUrl();
var string = toolName+" Authorization (then try formula again ),"+authorizationUrl;
var auth = [{}];
return string.split(",")
}
}
function getServiceGmail() {
return OAuth2.createService('Gmail')
.setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
.setTokenUrl('https://accounts.google.com/o/oauth2/token')
.setClientId('CLIENT-ID')
.setClientSecret('CLIENT-SECRET')
.setCallbackFunction('authCallback')
.setPropertyStore(PropertiesService.getDocumentProperties())
.setScope('https://mail.google.com/');
};
/**
* Handles the OAuth callback.
*/
function authCallback(request) {
var service = getServiceGmail();
var authorized = service.handleCallback(request);
if (authorized) {
return HtmlService.createHtmlOutput(
"<script>window.top.location.href='https://REDIRECT.com';</script>"
);
//return HtmlService.createHtmlOutput('PowerSheet authorization successful for your Google Sheet! You can close this tab.');
} else {
return HtmlService.createHtmlOutput('You did not provide authorization. Please close this tab and try again.');
}
}