I have this module, which allows for easy interaction with Blackboard Learn REST APIs in application code (I have not incorporated it yet).
var fs = require('fs');
var request = require('request');
exports.RestApp = function (origin, key, secret) {
var $blackboard = this;
$blackboard._key = key;
$blackboard._secret = secret;
$blackboard._auth = 'Basic ' + new Buffer($blackboard._key + ':' + $blackboard._secret).toString('base64');
$blackboard._origin = origin;
$blackboard._token = function (callback) {
var options = {
method: 'post',
url: $blackboard._origin + '/learn/api/public/v1/oauth2/token',
headers: {Authorization: $blackboard._auth},
form: {grant_type: 'client_credentials'},
json: true
};
var req = request(options, function (err, res, body) {
if (err) {
console.error(err);
} else {
$blackboard._token.accessToken = body.access_token;
if (callback) callback();
}
});
};
$blackboard._token.accessToken = '';
$blackboard._ajaxInner = function (method, endpoint, data, file, callback, hasFailed) {
var options = {
method: method,
url: encodeURI($blackboard._origin + '/learn/api/public/v1/' + endpoint.replace(/^\//, '')),
headers: {
Authorization: 'Bearer ' + $blackboard._token.accessToken,
'content-type':'application/json'
},
body: data,
formData: !!file ? {
toUpload: {
value: fs.createReadStream(file.path),
options: {
filename: file.originalFilename,
contentType: file.headers['content-type']
}
}
} : null,
json: true
};
request(options, function (err, res, body) {
var msg = (body || {}).message;
if (msg === 'API request is not authenticated.' || msg === 'Bearer token is invalid') {
if (!hasFailed) {
$blackboard._token(() => $blackboard._ajaxInner(method, endpoint, data, file, callback, true));
} else {
console.error('Authentication failed.');
}
} else {
callback(err, res, body);
}
});
};
$blackboard._ajax = function (method, endpoint, data, file, callback) {
if (typeof callback !== 'function') callback = (err, res, body) => console.log(err || body);
var innerFn = () => $blackboard._ajaxInner(method, endpoint, data, file, callback);
$blackboard._token.accessToken ? $blackboard._token(innerFn) : innerFn();
};
['get','post','patch','delete','put'].forEach(function (method) {
$blackboard[method] = function (endpoint, options) {
options = options || {};
if (typeof options === 'function') {
var callback = options;
$blackboard._ajax(method, endpoint, void 0, null, callback);
} else {
var data = options.body || options.data;
var file = options.file;
var callback = options.callback || options.complete;
$blackboard._ajax(method, endpoint, data, file, callback);
}
};
});
};
exports.restApp = exports.RestApp;
Also, I have a NODE.JS project, with which I want to integrate the above. This project has a classic structure, where I have the models, controllers and routes. What exactly do I have to add to be able to make the connection with blackboard, or at least verify that it works?
The structure of my project is as follows:
/src
- /controllers
- /database
- /middlewares
- /models
- /routes
- app.js
- bridge_bb_lc.js
.gitignore
bridge_bb_lc is the project and the file bridge_bb_lc.js contains:
var app = require('./app');
require('dotenv').config();
const PORT = process.env.PORT || 3012;
async function main(){
await app.listen(PORT);
console.log("Server on port " + PORT);
}
main();
An apology if this is a simple problem to solve, the thing is that I'm a little rusty when it comes to Node.