const crypto = require('crypto'); const https = require('https'); const API_TOKEN = 'your_apiEuro_token'; const SECRET_KEY = 'you_apiEuro_secretKey'; const DOMAIN = 'api.euro.pruefster.com'; /** * Will generate a signature * @param {Object} params * @returns signedString for api call */ function gen_Signature(params) { const paramsString = Params_Strings(params); return crypto.createHmac('sha256', SECRET_KEY).update(paramsString).digest('hex'); } /** * Sorts the params alphabetically * @param {Object} params * @returns sortedString thats needs to be signed */ function Params_Strings(params) { let string = ""; const keys = Object.keys(params); keys.sort(); for (let key of keys) { let value = typeof(params[key]) == "object" ? JSON.stringify(params[key]) : params[key]; string += key + "=" + value + "?"; } return Buffer.from(string.slice(0, -1)).toString("utf-8"); } function getExam() { // Generating the params: // All required and optional params have to be entered inside the params object // The only param that get entered inside the 'options.path' is the ':id' or ':exam_id' parameter depending on the api call made, // it is not required inside the params object for the signature. // For example: getting an exam with the id = 83 would require the following path: '/api/v1/exams/83', any additional parameters // have to be entered inside the 'params' object such as the 'timestamp', 'nonce' etc. to be signed. const params = { nonce: Math.round(Date.now() / 1000), timestamp: Date.now() //additonal examdata / studentdata etc.... }; signature = gen_Signature(params); params.signature = signature; const stringifiedParams = JSON.stringify(params); //New and necessary way to calculate the stringlength via buffer for the content-length in the request header (row: 72) const buffStr = Buffer.from(stringifiedParams, 'utf-8'); // path and method are essential for the api call, the path describes the unit that is called inside the Pruefster api, // the method describes the specified CRUD function that allows the user of the api to create (POST), read (GET), update (PUT,PATCH) and delete (DELETE) // the ressources described inside the documentation. const options = { hostname: DOMAIN, path: '/api/v1/exams/83', method: 'GET', headers: { 'Content-Type': 'application/json', 'Authorization': 'apiEuroToken ' + API_TOKEN, 'Accept': 'application/json', 'Content-Length': buffStr.byteLength } } const req = https.request(options, (res) => { console.log('Status code:', res.statusCode); res.on('data', (data) => { process.stdout.write(data); }); }); req.on('error', error => { console.error(error) }) req.write(stringifiedParams); req.end(); } getExam();