// Import the OAuth2 library
const OAuth2 = OAuth2.createService('microsoft')
.setClientId('{YOUR_CLIENT_ID}')
.setClientSecret('{YOUR_CLIENT_SECRET}')
.setCallbackFunction('authCallback')
.setPropertyStore(PropertiesService.getUserProperties());
// Function to initiate the authorization flow
function startAuth() {
const authorizationUrl = OAuth2.getAuthorizationUrl();
console.log('Authorization URL:', authorizationUrl);
}
// Callback function to receive the access token
function authCallback(request) {
const isAuthorized = OAuth2.handleCallback(request);
if (isAuthorized) {
console.log('Authorized successfully!');
} else {
console.log('Authorization failed.');
}
}
// Example usage
function exampleUsage() {
if (!OAuth2.hasAccess()) {
const authorizationUrl = OAuth2.getAuthorizationUrl();
console.log('Authorization URL:', authorizationUrl);
} else {
// Here you can make authenticated API calls to Microsoft
// using the access token
const accessToken = OAuth2.getAccessToken();
console.log('Access Token:', accessToken);
// Make API calls with the access token
// ...
}
}