2017-09-15 51 views
0

在Debian服务器中运行Django rest api并使用Tokbox。我通过一个运行良好的python虚拟环境运行它,但出于几个原因需要将它从环境中取出。当我这样做,installin所有的依赖后,得到它运行得到了以下错误:未运行venv时OpenTok构造函数/ create_session失败

raise RequestError('Failed to create session: %s' % str(e))

opentok.exceptions.RequestError: Failed to create session: Failed to create session, invalid credentials

两个密钥保存为环境变量和背部正确带来的,我可以登录他们,他们是正确的。另外,如果我再次打开python虚拟环境,错误消失。

为了记录在案,我的代码导致错误的线路有:

opentok = OpenTok(api_key, api_secret) 
session = opentok.create_session(media_mode=MediaModes.routed) 

这就在源代码中的异常的函数如下:

try: 
     response = requests.post(self.session_url(), data=options, headers=self.headers(), proxies=self.proxies) 
     response.encoding = 'utf-8' 

if response.status_code == 403: 
      raise AuthError('Failed to create session, invalid credentials') 

起初我认为它必须是对api密钥和api秘密进行某种加密或哈希处理。而且Tokbox确实使用了jwt,但是它在一个由构造函数调用的函数中完成,因此也在我不使用虚拟环境时完成。在上述请求调用的函数headers()如下:

def headers(self): 
    """For internal use.""" 
    return { 
     'User-Agent': 'OpenTok-Python-SDK/' + __version__ + ' ' + platform.python_version(), 
     'X-OPENTOK-AUTH': self._create_jwt_auth_header() 
    } 

def _create_jwt_auth_header(self): 
    payload = { 
        'ist': 'project', 
        'iss': self.api_key, 
        'iat': int(time.time()), # current time in unix time (seconds) 
        'exp': int(time.time()) + (60*3), # 3 minutes in the future (seconds) 
        'jti': '{0}'.format(0, random.random()) 
       } 

    return jwt.encode(payload, self.api_secret, algorithm='HS256') 
+0

,想到的唯一的事情是,也许当您使用使JWT过期或处于过去状态的其他环境时,时间不同步。那可能吗? –

+0

@AdamUllman日期和时间似乎正确无论在进出venv。任何其他想法? –

+0

@AdamUllman尽管时间似乎正确,但的确是时间问题。即将发布解决方案 –

回答

1

服务器时间似乎正确的,但我想这是不是。你可以通过在jwt函数中从'iat'减去一些时间来解决这个问题。感谢tokbox支持,终于找到解决方案,以支持他们!

寻找到你的安装设置文件“opentok.py”由:

find/-name opentok.py 

然后3600。减去像下面的线显示:

def _create_jwt_auth_header(self): 
    payload = { 
        'ist': 'project', 
        'iss': self.api_key, 
        'iat': int(time.time()) - 3600, # three minutes ago (seconds) 
        'exp': int(time.time()) + (60*3), # 3 minutes in the future (seconds) 
        'jti': '{0}'.format(0, random.random()) 
       } 

    return jwt.encode(payload, self.api_secret, algorithm='HS256') 
相关问题