Skip to content

Instantly share code, notes, and snippets.

@mauricionr
Last active April 5, 2019 19:38
Show Gist options
  • Save mauricionr/ce4c4af9eb845735a825 to your computer and use it in GitHub Desktop.
Save mauricionr/ce4c4af9eb845735a825 to your computer and use it in GitHub Desktop.

Revisions

  1. mauricionr revised this gist Dec 12, 2015. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions get-sharepoint-access-token-nodejs.js
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,6 @@
    var request = require('request');
    var jwt = require('jsonwebtoken')
    function getAccessToken(req) {
    /**
    * Code to get access token for sharepoint provider hosted apps in context token flow
    * Thanks both code above and TokenHelper.cs
    */
    //https://gist.github.com/demelziraptor/9039435 Python
    //http://www.getcodesamples.com/src/A39B1460/F692912D PHP
    req.session.sp_info = req.query;
  2. mauricionr revised this gist Sep 17, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion get-sharepoint-access-token-nodejs.js
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,6 @@ var request = require('request');
    var jwt = require('jsonwebtoken')
    function getAccessToken(req) {
    /**
    * Author:Mauricio Nunes dos reis
    * Code to get access token for sharepoint provider hosted apps in context token flow
    * Thanks both code above and TokenHelper.cs
    */
  3. mauricionr revised this gist Sep 17, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion get-sharepoint-access-token-nodejs.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ var jwt = require('jsonwebtoken')
    function getAccessToken(req) {
    /**
    * Author:Mauricio Nunes dos reis
    * Code to get access token for sharepoint provider hosted apps
    * Code to get access token for sharepoint provider hosted apps in context token flow
    * Thanks both code above and TokenHelper.cs
    */
    //https://gist.github.com/demelziraptor/9039435 Python
  4. mauricionr created this gist Sep 17, 2015.
    43 changes: 43 additions & 0 deletions get-sharepoint-access-token-nodejs.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    var request = require('request');
    var jwt = require('jsonwebtoken')
    function getAccessToken(req) {
    /**
    * Author:Mauricio Nunes dos reis
    * Code to get access token for sharepoint provider hosted apps
    * Thanks both code above and TokenHelper.cs
    */
    //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)
    }