The CreatorCon Call for Content is officially open! Get started here.

How to retrive the oauth token an by script

J_r_my1
Mega Guru

Hello everyone,

I have created and configured my Oauth provider, and when I go in the OAuth 2.0 Credentials, I am able to retrieve my token.

I wanted to retrieve this token by script in order to be able to perform some powershell action on my exchange server.

But i can't get my token by script, this is my current script but no luck

var SCOPE = "https://outlook.office365.com/.default" ;	
  var profileId = 'XXX';

  // createing request
  var client  = new sn_auth.GlideOAuthClient();
  var request = new sn_auth.GlideOAuthClientRequest(); 
  
    request.setProfile(profileId); 
	request.setGrantType('client_credentials');
	request.setScope(SCOPE);

  var resp = client.requestToken(request);
 

 if (!resp || !resp.isValid()) {
    gs.error('OAuth error: ' + (resp ? resp.getErrorMessage() : 'no response'));
    
  }

  var token = resp.getAccessToken();
  gs.info('Token OK, expires in ' + resp.getExpiresIn() + 's');

 

Does anyone know how I could do it ?

2 REPLIES 2

vignesh parthib
Tera Guru

Hi @J_r_my1 

 

Can you try this approach to retrieve token. 

var oauthClient = new sn_auth.GlideOAuthClient();
var tokenResult = oauthClient.getAccessToken('Your_OAuth_Profile_Name');

if (tokenResult.access_token) {
    var accessToken = tokenResult.access_token;
    gs.info("Access token successfully retrieved: " + accessToken);
} else {
    gs.error("Failed to retrieve access token");
}


Thanks,
Vignesh
"If this solution resolves your issue, kindly mark it as correct."

Doesn't work as well, i also tried the method

oauthClient.getToken();

But it doesn't work.