當前位置: 首頁>>代碼示例>>Python>>正文


Python OpenTok.generate_token方法代碼示例

本文整理匯總了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)
開發者ID:raghavtan,項目名稱:chat_server,代碼行數:30,代碼來源:views.py

示例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
開發者ID:laughingwb,項目名稱:CloudClass,代碼行數:17,代碼來源:models.py

示例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
開發者ID:fahyik,項目名稱:interprefy,代碼行數:53,代碼來源:views.py

示例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')
開發者ID:NAVEENMN,項目名稱:mbideo,代碼行數:31,代碼來源:hello.py

示例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)
開發者ID:opentok,項目名稱:Opentok-Python-SDK,代碼行數:86,代碼來源:test_token_generation.py


注:本文中的opentok.OpenTok.generate_token方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。