本文整理汇总了Python中opentok.OpenTok.generate_token方法的典型用法代码示例。如果您正苦于以下问题:Python OpenTok.generate_token方法的具体用法?Python OpenTok.generate_token怎么用?Python OpenTok.generate_token使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类opentok.OpenTok
的用法示例。
在下文中一共展示了OpenTok.generate_token方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: index
# 需要导入模块: from opentok import OpenTok [as 别名]
# 或者: from opentok.OpenTok import generate_token [as 别名]
def index(request):
key = "45351832"
secret = "29d4d704b3a28373202c230946bfe91e3e74c4fb"
opentok = OpenTok(key, secret)
if request.method == "POST":
name = request.POST.get('name', '')
role = request.POST.get('role', '')
if role == "": role = "Publisher"
ses_id = request.POST.get('session', '')
try:
if ses_id is "":
ses = "New Session"
session = opentok.create_session()
session_id = session.session_id
token = opentok.generate_token(session_id,role=Roles.publisher)
else:
ses = "Existing Session"
session_id = ses_id
if role == "moderator":
token = opentok.generate_token(session_id,role=Roles.moderator)
elif role == "subscriber":
token = opentok.generate_token(session_id,role=Roles.subscriber)
else:
token = "Wrong Role requested for existing session id"
data = {"user": name, "Session": ses, "role":role, "session:": {"Authentication": token, "Session ID": session_id}}
except Exception as e:
data = {"Exception": e.message, "Type": type(e)}
return JsonResponse(data)
示例2: get_token_id
# 需要导入模块: from opentok import OpenTok [as 别名]
# 或者: from opentok.OpenTok import generate_token [as 别名]
def get_token_id(self, username, role=ROLE_STUDENT):
opentok = OpenTok(api_key, api_secret)
session_id = self.session_id
if not session_id:
return None
# Generate a Token from just a session_id (fetched from a database)
connectionMetadata = '{"role":"' + role + '", "username":"' + username + '"}'
if role == self.ROLE_TUTOR:
token_id = opentok.generate_token(session_id, Roles.moderator, None, connectionMetadata)
else:
token_id = opentok.generate_token(session_id, Roles.publisher, None, connectionMetadata)
return token_id
示例3: gen_token
# 需要导入模块: from opentok import OpenTok [as 别名]
# 或者: from opentok.OpenTok import generate_token [as 别名]
def gen_token(request, session_pk=None):
# make curl to create session
# requrl = request.build_absolute_uri(reverse("tbox:create_session"))
# req = urllib2.Request(requrl)
# res = urllib2.urlopen(req)
# session_id = res.read()
# GET has to pass the session_pk or passed from room view
if request.method == "GET" or session_pk:
res = {}
if request.GET or session_pk:
if request.GET:
session = Session.objects.get(pk=request.GET["session_pk"])
else:
session = Session.objects.get(pk=session_pk)
session_id = session.session_id
print "token request for session and session id:"
print session, session_id
tbox = TokBox.objects.get(pk=1)
opentok = OpenTok(tbox.api_key, tbox.api_secret)
# set token parameters: role and data
print "request by user:"
print request.user.username
userprofile = UserProfile.objects.get(username=request.user.username)
params = {"role": None, "data": "name=" + request.user.username}
if userprofile.function == "Mod":
params["role"] = Roles.moderator
elif userprofile.function == "Pub":
params["role"] = Roles.publisher
elif userprofile.function == "Sub":
params["role"] = Roles.subscriber
token = opentok.generate_token(session_id, **params)
print token
res["token"] = token
res["success"] = {"status": True, "error": None}
if request.GET:
return HttpResponse(json.dumps(res))
else:
return json.dumps(res)
else:
res["success"] = {"status": False, "error": "no GET data"}
return HttpResponse(json.dumps(res))
elif request.method == "POST":
# need to update session table with connection count
# need to update connection table with connection id and data
# will be done through ajax post/tokbox js on room view
pass
示例4: OpenTok
# 需要导入模块: from opentok import OpenTok [as 别名]
# 或者: from opentok.OpenTok import generate_token [as 别名]
from opentok import OpenTok
opentok = OpenTok(45391562, ad29bbb303526b268937e661ae51dfdaf88a8307)
# Create a session that attempts to send streams directly between clients (falling back
# to use the OpenTok TURN server to relay streams if the clients cannot connect):
session = opentok.create_session()
from opentok import MediaModes
# A session that uses the OpenTok Media Router:
session = opentok.create_session(media_mode=MediaModes.routed)
# A session with a location hint
session = opentok.create_session(location=u'12.34.56.78')
# An automatically archived session:
session = opentok.create_session(media_mode=MediaModes.routed, archive_mode=ArchiveModes.always)
# Store this session ID in the database
session_id = session.session_id
# Generate a Token from just a session_id (fetched from a database)
token = opentok.generate_token(session_id)
# Generate a Token by calling the method on the Session (returned from create_session)
token = session.generate_token()
from opentok import Roles
# Set some options in a token
token = session.generate_token(role=Roles.moderator,
expire_time=int(time.time()) + 10,
data=u'name=Johnny')
示例5: OpenTokTokenGenerationTest
# 需要导入模块: from opentok import OpenTok [as 别名]
# 或者: from opentok.OpenTok import generate_token [as 别名]
class OpenTokTokenGenerationTest(unittest.TestCase):
def setUp(self):
self.api_key = u('123456')
self.api_secret = u('1234567890abcdef1234567890abcdef1234567890')
self.session_id = u('1_MX4xMjM0NTZ-flNhdCBNYXIgMTUgMTQ6NDI6MjMgUERUIDIwMTR-MC40OTAxMzAyNX4')
self.opentok = OpenTok(self.api_key, self.api_secret)
def test_generate_plain_token(self):
token = self.opentok.generate_token(self.session_id)
assert isinstance(token, text_type)
assert token_decoder(token)[u('session_id')] == self.session_id
assert token_signature_validator(token, self.api_secret)
def test_generate_role_token(self):
token = self.opentok.generate_token(self.session_id, Roles.moderator)
assert isinstance(token, text_type)
assert token_decoder(token)[u('role')] == Roles.moderator.value
assert token_signature_validator(token, self.api_secret)
token = self.opentok.generate_token(self.session_id, role=Roles.moderator)
assert isinstance(token, text_type)
assert token_decoder(token)[u('role')] == Roles.moderator.value
assert token_signature_validator(token, self.api_secret)
def test_generate_expires_token(self):
# an integer is a valid argument
expire_time = int(time.time()) + 100
token = self.opentok.generate_token(self.session_id, expire_time=expire_time)
assert isinstance(token, text_type)
assert token_decoder(token)[u('expire_time')] == text_type(expire_time)
assert token_signature_validator(token, self.api_secret)
# anything that can be coerced into an integer is also valid
expire_time = text_type(int(time.time()) + 100)
token = self.opentok.generate_token(self.session_id, expire_time=expire_time)
assert isinstance(token, text_type)
assert token_decoder(token)[u('expire_time')] == expire_time
assert token_signature_validator(token, self.api_secret)
# a datetime object is also valid
if PY2:
expire_time = datetime.datetime.fromtimestamp(time.time(), pytz.UTC) + datetime.timedelta(days=1)
if PY3:
expire_time = datetime.datetime.fromtimestamp(time.time(), datetime.timezone.utc) + datetime.timedelta(days=1)
token = self.opentok.generate_token(self.session_id, expire_time=expire_time)
assert isinstance(token, text_type)
assert token_decoder(token)[u('expire_time')] == text_type(calendar.timegm(expire_time.utctimetuple()))
assert token_signature_validator(token, self.api_secret)
def test_generate_data_token(self):
data = u('name=Johnny')
token = self.opentok.generate_token(self.session_id, data=data)
assert isinstance(token, text_type)
assert token_decoder(token)[u('connection_data')] == data
assert token_signature_validator(token, self.api_secret)
def test_generate_initial_layout_class_list(self):
initial_layout_class_list = [u('focus'), u('small')];
token = self.opentok.generate_token(self.session_id, initial_layout_class_list=initial_layout_class_list)
assert isinstance(token, text_type)
assert sorted(token_decoder(token)[u('initial_layout_class_list')].split(u(' '))) == sorted(initial_layout_class_list)
assert token_signature_validator(token, self.api_secret)
def test_generate_no_data_token(self):
token = self.opentok.generate_token(self.session_id)
assert isinstance(token, text_type)
assert u('connection_data') not in token_decoder(token)
assert token_signature_validator(token, self.api_secret)
@raises(TypeError)
def test_does_not_generate_token_without_params(self):
token = self.opentok.generate_token()
@raises(TypeError)
def test_does_not_generate_token_without_session(self):
token = self.opentok.generate_token(role=Roles.subscriber)
@raises(OpenTokException)
def test_does_not_generate_token_invalid_session(self):
token = self.opentok.generate_token(u('NOT A REAL SESSIONID'))
@raises(OpenTokException)
def test_does_not_generate_token_without_api_key_match(self):
# this session_id has the wrong api_key
session_id = u('1_MX42NTQzMjF-flNhdCBNYXIgMTUgMTQ6NDI6MjMgUERUIDIwMTR-MC40OTAxMzAyNX4')
token = self.opentok.generate_token(session_id)