Suggestions

close search

Connection Token Creation — Ruby

In order to authenticate a user connecting to an OpenTok session, a client must connect using a token (see this overview).

The following Ruby code example shows how to generate a token using the OpenTok Ruby server-side library:

class SampleController < ApplicationController
    def action
        # Replace with your OpenTok API key:
        api_key = "your_API_key"
        # Replace with your OpenTok API secret:
        api_secret = "your_API_secret"

        opentok = OpenTok::OpenTok.new api_key, api_secret
        session = opentok.create_session
        token = session_id.generate_token
    end
end

Calling the generate_token() method returns a string. This string is the token.

The following Ruby code example shows how to obtain a token that has a role of "publisher" and that has a connection metadata string:

class SampleController < ApplicationController
    def action
        # Replace with your OpenTok API key:
        api_key = "your_API_key"
        # Replace with your OpenTok API secret:
        api_secret = "your_API_secret"

        opentok = OpenTok::OpenTok.new api_key, api_secret
        session = opentok.create_session

        token = opentok.generate_token :role => :publisher :data "username=bob"
    end
end

The method takes the following options: