I am using Oauth2 to connect to the Microsoft Graph API with Google Apps Script. First, I click on the url that comes with the authorize() function and gain access. But after a certain period of time, access expires and I need to gain access with the authorize() function again. How can I ensure that access is always active? I think refresh tokens are used, but I couldn’t get the result I wanted.
My code:
<code>var CLIENT_ID = 'CLIENT_ID';
var CLIENT_SECRET = 'CLIENT_SECRET';
var REDIRECT_URI = 'https://script.google.com/macros/d/PROJECT_ID/usercallback';
var TOKEN_URL = 'https://login.microsoftonline.com/common/oauth2/v2.0/token';
var AUTH_URL = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize';
var SCOPE = 'https://graph.microsoft.com/.default';
function getOAuthService() {
return OAuth2.createService('MicrosoftGraph')
.setAuthorizationBaseUrl(AUTH_URL)
.setClientSecret(CLIENT_SECRET)
.setCallbackFunction('authCallback')
.setPropertyStore(PropertiesService.getUserProperties())
.setParam('access_type', 'offline') // Yenileme token'ı almak için gerekli
.setParam('prompt', 'consent');
function authCallback(request) {
var service = getOAuthService();
var isAuthorized = service.handleCallback(request);
return HtmlService.createHtmlOutput('Authorization successful. You can close this tab.');
return HtmlService.createHtmlOutput('Authorization failed.');
var service = getOAuthService();
if (!service.hasAccess()) {
var authorizationUrl = service.getAuthorizationUrl();
Logger.log('Open the following URL and authorize the app: ' + authorizationUrl);
// E-posta ile gönderme örneği
MailApp.sendEmail(Session.getActiveUser().getEmail(), 'Authorize Google Apps Script', 'Open the following URL and authorize the app: ' + authorizationUrl);
function fetchMicrosoftGraphData() {
var service = getOAuthService();
if (service.hasAccess()) {
var url = 'https://graph.microsoft.com/v1.0/me'; // Örnek Microsoft Graph API çağrısı
var response = UrlFetchApp.fetch(url, {
Authorization: 'Bearer ' + service.getAccessToken()
var result = JSON.parse(response.getContentText());
Logger.log('No access. Re-authorizing...');
getOAuthService().reset();
<code>var CLIENT_ID = 'CLIENT_ID';
var CLIENT_SECRET = 'CLIENT_SECRET';
var REDIRECT_URI = 'https://script.google.com/macros/d/PROJECT_ID/usercallback';
var TOKEN_URL = 'https://login.microsoftonline.com/common/oauth2/v2.0/token';
var AUTH_URL = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize';
var SCOPE = 'https://graph.microsoft.com/.default';
function getOAuthService() {
return OAuth2.createService('MicrosoftGraph')
.setAuthorizationBaseUrl(AUTH_URL)
.setTokenUrl(TOKEN_URL)
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
.setCallbackFunction('authCallback')
.setPropertyStore(PropertiesService.getUserProperties())
.setScope(SCOPE)
.setParam('access_type', 'offline') // Yenileme token'ı almak için gerekli
.setParam('prompt', 'consent');
}
function authCallback(request) {
var service = getOAuthService();
var isAuthorized = service.handleCallback(request);
if (isAuthorized) {
return HtmlService.createHtmlOutput('Authorization successful. You can close this tab.');
} else {
return HtmlService.createHtmlOutput('Authorization failed.');
}
}
function authorize() {
var service = getOAuthService();
if (!service.hasAccess()) {
var authorizationUrl = service.getAuthorizationUrl();
Logger.log('Open the following URL and authorize the app: ' + authorizationUrl);
// E-posta ile gönderme örneği
MailApp.sendEmail(Session.getActiveUser().getEmail(), 'Authorize Google Apps Script', 'Open the following URL and authorize the app: ' + authorizationUrl);
}
}
function fetchMicrosoftGraphData() {
var service = getOAuthService();
if (service.hasAccess()) {
var url = 'https://graph.microsoft.com/v1.0/me'; // Örnek Microsoft Graph API çağrısı
var response = UrlFetchApp.fetch(url, {
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
}
});
var result = JSON.parse(response.getContentText());
Logger.log(result);
} else {
Logger.log('No access. Re-authorizing...');
authorize();
}
}
function reset() {
getOAuthService().reset();
}
</code>
var CLIENT_ID = 'CLIENT_ID';
var CLIENT_SECRET = 'CLIENT_SECRET';
var REDIRECT_URI = 'https://script.google.com/macros/d/PROJECT_ID/usercallback';
var TOKEN_URL = 'https://login.microsoftonline.com/common/oauth2/v2.0/token';
var AUTH_URL = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize';
var SCOPE = 'https://graph.microsoft.com/.default';
function getOAuthService() {
return OAuth2.createService('MicrosoftGraph')
.setAuthorizationBaseUrl(AUTH_URL)
.setTokenUrl(TOKEN_URL)
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
.setCallbackFunction('authCallback')
.setPropertyStore(PropertiesService.getUserProperties())
.setScope(SCOPE)
.setParam('access_type', 'offline') // Yenileme token'ı almak için gerekli
.setParam('prompt', 'consent');
}
function authCallback(request) {
var service = getOAuthService();
var isAuthorized = service.handleCallback(request);
if (isAuthorized) {
return HtmlService.createHtmlOutput('Authorization successful. You can close this tab.');
} else {
return HtmlService.createHtmlOutput('Authorization failed.');
}
}
function authorize() {
var service = getOAuthService();
if (!service.hasAccess()) {
var authorizationUrl = service.getAuthorizationUrl();
Logger.log('Open the following URL and authorize the app: ' + authorizationUrl);
// E-posta ile gönderme örneği
MailApp.sendEmail(Session.getActiveUser().getEmail(), 'Authorize Google Apps Script', 'Open the following URL and authorize the app: ' + authorizationUrl);
}
}
function fetchMicrosoftGraphData() {
var service = getOAuthService();
if (service.hasAccess()) {
var url = 'https://graph.microsoft.com/v1.0/me'; // Örnek Microsoft Graph API çağrısı
var response = UrlFetchApp.fetch(url, {
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
}
});
var result = JSON.parse(response.getContentText());
Logger.log(result);
} else {
Logger.log('No access. Re-authorizing...');
authorize();
}
}
function reset() {
getOAuthService().reset();
}
I wanted the tokens to be constantly renewed, but they were not renewed and I had to manually run the authorise() function to gain access, click on the link it gave and gain access. Continuous access should be active without my intervention.