Skip to content

Instantly share code, notes, and snippets.

@benjameep
Created January 22, 2018 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjameep/88565b31928429aa43d247c9250aa67c to your computer and use it in GitHub Desktop.
Save benjameep/88565b31928429aa43d247c9250aa67c to your computer and use it in GitHub Desktop.
Make d2l api calls from the command line using account creds, use this function to get the cookies
var request = require('request').defaults({jar: true})
function login(username, password, cb){
request.post({
// This is the first url sent when you press login
url: 'https://byui.brightspace.com/d2l/lp/auth/login/login.d2l',
form: {
userName: username,
password: password,
},
// Which immediately redirects to something else, so need to handle that
followAllRedirects: true,
}, function (err, res, body) {
if (err) {return cb(err)}
// That redirected page returns html which only contains javascript which changes the window.location to this url, so we need to go there for them to validate our session cookies
var lastUrl = 'https://byui.brightspace.com/d2l/lp/auth/login/ProcessLoginActions.d2l';
request.get(lastUrl, function(err, res, body) {
if (err) {return cb(err)}
// We are all set to use the api
cb(null)
})
})
}
const USERNAME = 'cct_username'
const PASSWORD = 'cct_password'
login(USERNAME,PASSWORD,function(err){
if(err) return console.error(err)
// Now you can just do requests to the api, and the request library handles the cookies for you
var apiEndpoint = 'https://byui.brightspace.com/d2l/api/lp/1.20/10011/groupcategories/'
request.get(apiEndpoint,(err,res,body) => {
if(err) return console.error(err)
console.log(body)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment