当不是 运行 venv 时,OpenTok constructor/create_session 失败
OpenTok constructor/create_session fails when not running venv
运行 Debian 服务器中的 Django rest api 和 Tokbox。我是 运行 通过 python 虚拟环境运行的,该环境运行良好,但由于某些原因需要将其从环境中移除。当我这样做时,在安装所有依赖项并得到它后 运行 出现以下错误:
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')
服务器时间似乎是正确的,但我猜不是。您可以通过在 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')
运行 Debian 服务器中的 Django rest api 和 Tokbox。我是 运行 通过 python 虚拟环境运行的,该环境运行良好,但由于某些原因需要将其从环境中移除。当我这样做时,在安装所有依赖项并得到它后 运行 出现以下错误:
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')
服务器时间似乎是正确的,但我猜不是。您可以通过在 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')