Google Apps Script Oauth2 Continuous Access

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật