Created
March 19, 2020 07:44
-
-
Save olegpolyakov/179777f2e7e0d10db6ed188ba45091ee to your computer and use it in GitHub Desktop.
Zoom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const crypto = require('crypto'); | |
const request = require('request'); | |
const jwt = require('jsonwebtoken'); | |
const ZOOM_API_URL = 'https://api.zoom.us/v2/'; | |
function getToken(key, secret) { | |
return jwt.sign({ | |
iss: key, | |
exp: Math.floor(Date.now() / 1000) + 60 | |
}, secret); | |
} | |
module.exports = (ZOOM_API_KEY, ZOOM_API_SECRET) => { | |
function zoomRequest({ path, method = 'GET', body }) { | |
return new Promise((resolve, reject) => { | |
request({ | |
url: `${ZOOM_API_URL}${path}`, | |
method, | |
auth: { | |
bearer: getToken(ZOOM_API_KEY, ZOOM_API_SECRET) | |
}, | |
json: true, | |
body | |
}, (error, response, body) => { | |
if (error) { | |
reject(error); | |
} else { | |
resolve(body); | |
} | |
}); | |
}); | |
} | |
return { | |
generateSignature: (meetingNumber, role = 0) => { | |
const time = Date.now() - 1000; | |
const data = Buffer.from(`${ZOOM_API_KEY}${meetingNumber}${time}${role}`).toString('base64'); | |
const hash = crypto.createHmac('sha256', ZOOM_API_SECRET).update(data).digest('base64'); | |
const signature = Buffer.from(`${ZOOM_API_KEY}.${meetingNumber}.${time}.${role}.${hash}`).toString('base64'); | |
return encodeURI(signature.replace('=', '')); | |
}, | |
users: { | |
create: ({ email, firstname: first_name, lastname: last_name, type = 1 }) => { | |
if (!email) return Promise.reject('Email is required to create a user on Zoom'); | |
return zoomRequest({ | |
path: '/users', | |
method: 'POST', | |
body: { | |
action: 'create', | |
user_info: { | |
email, | |
first_name, | |
last_name, | |
type | |
} | |
} | |
}); | |
}, | |
activate: userId => zoomRequest({ | |
path: `/users/${userId}/status`, | |
method: 'PUT', | |
body: { | |
action: 'activate' | |
} | |
}), | |
deactivate: userId => zoomRequest({ | |
path: `/users/${userId}/status`, | |
method: 'PUT', | |
body: { | |
action: 'deactivate' | |
} | |
}) | |
}, | |
webinars: { | |
list: () => zoomRequest({ | |
path: '/users/UuLuEyiQTAa1U-U-NgRHOA/webinars', | |
method: 'GET' | |
}), | |
register: (webinarId, data) => zoomRequest({ | |
path: `webinars/${webinarId}/registrants`, | |
method: 'POST', | |
body: { | |
email: data.email, | |
first_name: data.firstname, | |
last_name: data.lastname | |
} | |
}), | |
unregister: (webinarId, registrantId) => zoomRequest({ | |
path: `webinars/${webinarId}/registrants/status`, | |
method: 'PUT', | |
body: { | |
action: 'cancel', | |
registrants: [{ id: registrantId }] | |
} | |
}) | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment