Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save princeppy/5943edd94dc1875f6e1dca918b996367 to your computer and use it in GitHub Desktop.
Save princeppy/5943edd94dc1875f6e1dca918b996367 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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment