12
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

zoomusを使いたい

Last updated at Posted at 2018-09-23

zoomusとは

zoomのapi?

zoomとは

これ
https://zoom.us/jp-jp/meetings.html
Skypeみたいに通話できるツール

やりたいこと(できていない)

zoomusのapiを使ってzoomを利用(誰かと通話)すること

zoomのドキュメント

言語

Ruby
理由:とあるサービスをRailsで作る予定だから

GitHubにあったライブラリ

おそらくやること

・JWTでトークンを取得
・トークンからuser_idを取得?
・user_idlから通話?

やったこと

http://r9.hateblo.jp/entry/2018/01/30/081534
これを見る限りは
・JWTでトークンを取得
・トークンからuser_idを取得?
・user_idlから通話?
なんだけどなぁ。。。同じことをrubyでどうやってできるのだろうか

JWT

Rubyでjwtを利用する
https://blog.shimar.me/2017/02/10/ruby-jwt.html

zoom APIでの利用はHS256

Warning
The Zoom API uses HS256. Use of other algorithms may produce unexpected results.

Claimはissとexp

  • iss:zoom APIキー
  • exp :有効期限:現在の時刻から+整数

Note
The expiration time (exp) can be defined in a numeric date time format.

expは日付形式

https://github.com/jwt/ruby-jwt
これのpayloadでClaimを設定すればOKっぽい=> JWTはクリアできそう
以下の内容でJWTは生成できた

module ApplicationHelper
	class TS_Zoom
		def GenerateJWT
			payload = { 
				iss: 'api_key', #api_key
				exp: Time.now.to_i + 4 * 3600
			}
			secret  = 'secret_key' #secret_key
			token = JWT.encode payload, secret, 'HS256'
			return token
		end
	end
end

しかし、ターミナルで以下を打っても(23) Failed writing body と出る
curl -H 'Authorization: Bearer JWTの文字列' https://api.zoom.us/v2/users | jq .
ターミナルでcurl -H 'Authorization: Bearer JWTの文字列' https://api.zoom.us/v2/usersを打てばユーザ情報がでる

あとは、リクエストをRubyでどう扱うか、、、
https://docs.ruby-lang.org/ja/latest/class/Net=3a=3aHTTP.html
が使えるようです。
POSTの使い方はこちらかな
https://qiita.com/seisonshi/items/c23c0154c45ccbfa9999

以下のコード書いて実行してみた。

module ApplicationHelper
	class TS_Zoom
		def initialize
			@api_key = "aaa"
			@secret  = "bbb"
			@user_id = "ccc"
			@jwt = "ddd"
			@meeting_url = "eee"
		end
		def GenerateJWT
			payload = { 
				iss: @api_key,
				exp: Time.now.to_i + 4 * 36000
			}
			token = JWT.encode payload, @secret, 'HS256'
			return token
		end

		def GetUserData(_token)
			uri = URI.parse("https://api.zoom.us/v2/users")
			request = Net::HTTP::Get.new(uri)
			request["Authorization"] = "Bearer "+_token
			req_options = {
				use_ssl: uri.scheme == "https",
			}
			response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
				http.request(request)
			end
			result = JSON.parse(response.body)
			return result["users"][0]["id"].to_s
		end

		def GetMeetingURL
			@jwt = self.GenerateJWT
			@user_id = self.GetUserData(@jwt)
			@meeting_url = "https://api.zoom.us/v2/users/#{@user_id}/meetings"

		    uri = URI.parse(@meeting_url)
		    http = Net::HTTP.new(uri.host, uri.port)

		    http.use_ssl = true
		    http.verify_mode = OpenSSL::SSL::VERIFY_NONE

		    req = Net::HTTP::Post.new(uri.path)
			req["Authorization"] = "Bearer #{@jwt}"
			aa = "Bearer #{@jwt}"
			puts aa
			req["Content-Type"] = "application/json"
		    req.body = {
		    	"type":1,
		    	}.to_json
		    res = http.request(req)
		    puts res.class
		    puts res.code
		    puts res.body
		    return res
		end
	end
end

結果
ホストのURLとJoinのURLが表示されアクセスすると通話成功!

12
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?