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] + '/' + ".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) }