Suggestions

close search

Connection Token Creation — Python

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

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

import OpenTok

# Replace with your OpenTok API key:
api_key = "your_API_key"
# Replace with your OpenTok API secret:
api_secret = "your_API_secret"
# Replace with the representative URL of your session:
session_address = "192.0.43.10"

opentok_sdk = OpenTok.OpenTok(api_key, api_secret)
session = opentok_sdk.create_session(session_address)
token = opentok_sdk.generate_token(session.session_id)
print token

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

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

import OpenTok

# Replace with your OpenTok API key:
api_key = "your_API_key"
# Replace with your OpenTok API secret:
api_secret = "your_API_secret"
# Replace with the representative URL of your session:
session_address = "192.0.43.10"

opentok_sdk = OpenTok.OpenTok(api_key, api_secret)
role_constants = OpenTok.RoleConstants

session = opentok_sdk.create_session(session_address)
connectionMetadata = "username=Bob,userLevel=4"
token = opentok_sdk.generate_token(session.session_id, role_constants.PUBLISHER, None, connectionMetadata)
print token

The method takes the following arguments: