Skip to content

Instantly share code, notes, and snippets.

@mauricionr
Last active April 5, 2019 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mauricionr/ce4c4af9eb845735a825 to your computer and use it in GitHub Desktop.
Save mauricionr/ce4c4af9eb845735a825 to your computer and use it in GitHub Desktop.
var request = require('request');
var jwt = require('jsonwebtoken')
function getAccessToken(req) {
//https://gist.github.com/demelziraptor/9039435 Python
//http://www.getcodesamples.com/src/A39B1460/F692912D PHP
req.session.sp_info = req.query;
req.session.sp_oauth = req.body;
var client_ID = "{{clientID}}"
var appSecret = '{{appSecret}}';
var decoded = jwt.decode(req.session.sp_oauth.SPAppToken, appSecret)
decoded.appctx = JSON.parse(decoded.appctx);
var appctx = decoded.appctx;
var options = {
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
form: {
"grant_type": "refresh_token",
"client_id": client_ID + '@' + decoded['appctxsender'].split('@')[1],
"client_secret": appSecret,
"refresh_token": decoded.refreshtoken,
"resource": decoded['appctxsender'].split('@')[0] + '/' + "<domain>.sharepoint.com" + '@' + decoded['appctxsender'].split('@')[1]
}
};
console.log('\noptions\n', JSON.stringify(options));
var callback = function (req, res, body) {
console.log(JSON.stringify(body));
}
request.post(appctx.SecurityTokenServiceUri, options, callback)
}
@venkatkonjeti
Copy link

Hi,
Thanks for sharing valuable information. I am wondering how do we get the user context token rather than app context token.

Thanks,
Venkat Konjeti

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment