Created
February 11, 2018 00:20
-
-
Save pdornier/3465429d0f893ea317e62d4935072d59 to your computer and use it in GitHub Desktop.
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
var request = require('request'); | |
const CLIENT_ID = '<client_id>'; | |
const CLIENT_SECRET = '<client_secret>'; | |
const REDIRECT_URI = '<redirect_uri>'; | |
module.exports = { | |
// Handles the the redirect URI location | |
callback: function(req, res) { | |
// code is the OAuth code received from the Zoom OAuth permissions page | |
if (req.query.code) { | |
let url = 'https://zoom.us/oauth/token?grant_type=authorization_code&code=' + req.query.code + '&redirect_uri=' + REDIRECT_URI; | |
request.post(url, function(err, response, body) { | |
if (err) { | |
// Log the error and inform the user that authentication has failed | |
} | |
body = JSON.parse(body); | |
if (body.access_token) { | |
// Associate the Zoom authentication details (access_token, refresh_token, scope) with the user | |
// If the user has already connected Zoom before, update the authentication details | |
// Finally, inform the user that Zoom has been successfully connected | |
} else { | |
// Log the error and inform the user that authentication has failed | |
} | |
}).auth(CLIENT_ID, CLIENT_SECRET); | |
} | |
// If no code provided, need to redirect the user to the Zoom OAuth permissions page | |
res.redirect('https://zoom.us/oauth/authorize?response_type=code&client_id=' + CLIENT_ID + '&redirect_uri=' + REDIRECT_URI); | |
}, | |
/** | |
* Retrieves a new access token from the Zoom API using the refresh token. | |
* @param {Object} credential - the user's oauth credentials for zoom. | |
* @param {string} credential.accessToken - the expired Zoom access token | |
* @param {string} credential.refreshToken - the Zoom refresh token | |
* @param {function} callback - the callback that passes along the fresh access token or error message | |
*/ | |
_refreshAccessToken: function(credential, callback) { | |
let url = 'https://zoom.us/oauth/token?grant_type=refresh_token&refresh_token=' + credential.refreshToken + '&redirect_uri=' + REDIRECT_URI; | |
request.post(url, function(err, response, body) { | |
if (err) { | |
// Log the error | |
} | |
if (response.statusCode == 400 || response.statusCode == 401) { | |
// Request for a new access_token has failed - this likely means that the user has revoked Zoom account access from our app | |
// Gracefully handle the failure, and prompt the user to re-authenticate their Zoom account | |
} | |
var parsedBody = JSON.parse(body); | |
// Save the new access_token and refresh_token for future use and use the callback | |
}).auth(CLIENT_ID, CLIENT_SECRET); | |
}, | |
/** | |
* Creates an example Zoom meeting for the user. | |
* @param {Object} credential - the user's oauth credentials for zoom. | |
* @param {string} credential.accessToken - the expired Zoom access token | |
* @param {string} credential.refreshToken - the Zoom refresh token | |
* @param {function} callback - the callback that passes along the created Zoom meeting or error message | |
*/ | |
createMeeting: function(credential, callback) { | |
var self = this; | |
var tokenRetries = 3; | |
var makeRequest = function(accessToken) { | |
tokenRetries--; | |
if (!tokenRetries) { | |
// Attempts to create a meeting have failed after multiple attempts -> log the error and inform the user | |
} | |
request({ | |
url: 'https://api.zoom.us/v2/users/me/meetings', | |
method: 'POST', | |
json: { | |
'topic': 'Zoom example meeting', | |
'start_time': '2018-02-10T08:00:45+00:00', | |
'duration': 30 | |
}, | |
headers: { | |
'content-type': 'application/json' | |
} | |
}, function(err, response, body) { | |
if (err || !response || response.statusCode == 400) { | |
// Handle the error | |
} | |
if (response.statusCode === 401) { | |
// accessToken has expired - need to request a new access token using the refresh token | |
self._refreshAccessToken(credential, function(err, updatedCredential) { | |
if (err || !updatedCredential) { | |
// Handle the error | |
} | |
// Retry using the fresh access token | |
makeRequest(updatedCredential.accessToken); | |
}); | |
} else { | |
return callback(null, body); | |
} | |
}).auth(null, null, true, accessToken); | |
} | |
makeRequest(credential.accessToken); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment