Integrating Zoom API Version 2 to Ruby on Rails
If you have ever integrated zoom into your rails app with zoom version 1 API, you must have come across zoomus ruby gem. Zoomus uses version 1 API which is no longer supported. Hence, it is not recommended to build your new service on top of that old version. Zoom has also moved their developer tools to market place from the developers' site.
In this post, we shall be making a POST request to zoom to create a zoom meeting.
As a pre-requisite, You must have created your account in the marketplace since we are using API Version 2.
Get the following credentials, save them as environment variables:
ZOOM_US_USER_EMAIL
ZOOM_US_API_KEY
ZOOM_US_API_SECRET
Please keep in mind that Zoom API Version 2 uses JWT Authentication. Hence we shall configure our JWT token and set the expiry.
Please ensure you have the following on your Gemfile
gem ‘jwt’
gem ‘openssl’
Let’s begin:
grab env variables:
USER_ID = ENV[‘ZOOM_US_USER_EMAIL’]
API_KEY = ENV[‘ZOOM_US_API_KEY’]
API_SECRET = ENV[‘ZOOM_US_API_SECRET’]
create a payload for JWT:
PAYLOAD = { iss: API_KEY,exp: 1.hour.from_now.to_i }
Notice we set the expiry to 1 hour. You would need to extend it much more on a production app.
Or better still create a permanent token to use for all your request.
For permanent token you could use it like this:
token = ENV[‘ZOOM_TOKEN’]
Let’s continue
Automatically creates header and return JWT
token = JWT.encode(PAYLOAD, API_SECRET, ‘HS256’, {typ: ‘JWT’})
make url to desired enpoint
url = URI(“https://api.zoom.us/v2/users/#{USER_ID}/meetings")
Create the HTTP objects
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
we are making a post request
request = Net::HTTP::Post.new(url)
set authorization header
request[“authorization”] = “Bearer #{token}”
define content type
request[“content-type”] = ‘application/json’
convert request body to JSON object
request.body = meeting_params.to_json
Send the request
res = http.request(request)
parse response.
res = JSON.parse(res.body)
And that is it. You could now go ahead and do some database calls to save some data for future use.
Below is a code sample of how I made my zoom POST requests to create meetings.
class ZoomPost
require ‘net/http’
require ‘json’
USER_ID = ENV[‘ZOOM_US_USER_EMAIL’]
API_KEY = ENV[‘ZOOM_US_API_KEY’]
API_SECRET = ENV[‘ZOOM_US_API_SECRET’]
PAYLOAD = {
iss: API_KEY,
exp: 1.hour.from_now.to_i
}
def self.post_meeting(meeting_params, user_id, virtual_class_id)
#Automatically creates header and return JWT
token = JWT.encode(PAYLOAD, API_SECRET, ‘HS256’, {typ: ‘JWT’})
#token = ENV[‘ZOOM_TOKEN’]
url = URI(“https://api.zoom.us/v2/users/#{USER_ID}/meetings")
# Create the HTTP objects
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(url)
request[“authorization”] = “Bearer #{token}”
request[“content-type”] = ‘application/json’
request.body = meeting_params.to_json
# Send the request
res = http.request(request)
res = JSON.parse(res.body)
ZoomMeeting.create(meeting_id: res[‘id’],
start_url: res[‘start_url’],
join_url: res[‘join_url’],
duration: res[‘duration’],
time_created: res[‘created_at’],
time_zone: res[‘time_zone’],
topic: res[‘topic’],
host_id: res[‘host_id’],
user_id: user_id,
virtual_class_id: virtual_class_id)
end
end