我正在尝试使用 vonage 语音 api 模拟语音通话。 我正在尝试使用 python 来做到这一点。 我创建了一个 .env 文件并更新了应用程序 id 和私钥值的值,而不是路径(不确定从哪里获取它)。
这是下面编写的代码:
#!/usr/bin/env python3
import os
from os.path import join, dirname
from pprint import pprint
import vonage
from dotenv import load_dotenv
dotenv_path = join(dirname(__file__), "../.env")
load_dotenv(dotenv_path)
VONAGE_APPLICATION_ID = os.environ.get("VONAGE_APPLICATION_ID")
VONAGE_APPLICATION_PRIVATE_KEY_PATH = os.environ.get("VONAGE_APPLICATION_PRIVATE_KEY_PATH")
FROM_NUMBER = os.environ.get("FROM_NUMBER")
TO_NUMBER = os.environ.get("TO_NUMBER")
client = vonage.Client(
application_id=VONAGE_APPLICATION_ID,
private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH,
)
response = client.voice.create_call({
'to': [{'type': 'phone', 'number': TO_NUMBER}],
'from': {'type': 'phone', 'number': FROM_NUMBER},
'answer_url': ['https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json']
})
pprint(response)
我得到的错误如下:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/vonage/client.py", line 396, in _generate_application_jwt
return self._jwt_client.generate_application_jwt(self._jwt_claims)
AttributeError: 'Client' object has no attribute '_jwt_client'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "make-an-outbound-call.py", line 24, in <module>
'answer_url': ['https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json']
File "/usr/local/lib/python3.7/site-packages/vonage/voice.py", line 28, in create_call
return self._client.post(self._client.api_host(), "/v1/calls", params or kwargs, auth_type=Voice.auth_type)
File "/usr/local/lib/python3.7/site-packages/vonage/client.py", line 246, in post
self._request_headers['Authorization'] = self._create_jwt_auth_string()
File "/usr/local/lib/python3.7/site-packages/vonage/client.py", line 392, in _create_jwt_auth_string
return b"Bearer " + self._generate_application_jwt()
File "/usr/local/lib/python3.7/site-packages/vonage/client.py", line 400, in _generate_application_jwt
'JWT generation failed. Check that you passed in valid values for "application_id" and "private_key".'
vonage.errors.ClientError: JWT generation failed. Check that you passed in valid values for "application_id" and "private_key".```
错误消息“JWT 生成失败。检查是否为“application_id”和“private_key”传递了有效值”以及 AttributeError:“Client”对象没有属性“_jwt_client”,表明的私钥的实现不正确在你的 Python 代码中使用 Vonage 语音 API。不应该直接将私钥作为字符串传递。相反,Vonage Python 库希望提供私钥文件的 路径 。
以下是更正代码的分步指南:
- 存储的私钥:
-
将的
private.key
文件(从 Vonage 仪表板的“创建应用程序”部分下载)放在的项目目录中。出于安全原因,不要将此文件提交到的版本控制系统。 -
更新的
.env
文件: -
从
.env
文件中删除VONAGE_APPLICATION_PRIVATE_KEY_PATH
变量。 -
添加一个名为
VONAGE_APPLICATION_PRIVATE_KEY_FILE
的新变量,并在其中存储private.key
文件的路径,例如:VONAGE_APPLICATION_ID="YOUR_VONAGE_APPLICATION_ID" VONAGE_APPLICATION_PRIVATE_KEY_FILE="path/to/your/private.key" FROM_NUMBER="YOUR_VONAGE_NUMBER" TO_NUMBER="RECIPIENT_NUMBER"
-
更正的 Python 代码:
-
使用
VONAGE_APPLICATION_PRIVATE_KEY_FILE
环境变量加载私钥文件路径。 -
使用此路径在初始化
vonage.Client
时传递私钥。```python #!/usr/bin/env python3 import os from os.path import join, dirname from pprint import pprint import vonage from dotenv import load_dotenv
dotenv_path = join(dirname( file ), "../.env") load_dotenv(dotenv_path)
VONAGE_APPLICATION_ID = os.environ.get("VONAGE_APPLICATION_ID") VONAGE_APPLICATION_PRIVATE_KEY_FILE = os.environ.get("VONAGE_APPLICATION_PRIVATE_KEY_FILE") FROM_NUMBER = os.environ.get("FROM_NUMBER") TO_NUMBER = os.environ.get("TO_NUMBER")
client = vonage.Client( application_id=VONAGE_APPLICATION_ID, private_key=open(VONAGE_APPLICATION_PRIVATE_KEY_FILE, 'r').read(), )
response = client.voice.create_call({ 'to': [{'type': 'phone', 'number': TO_NUMBER}], 'from': {'type': 'phone', 'number': FROM_NUMBER}, 'answer_url': ['https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json'] })
pprint(response) ```
通过这些更改,的代码现在应该能够正确加载的私钥,并且应该不再遇到与 JWT 相关的错误。始终记住将的私钥保密,切勿将其提交到公共代码存储库。
标签:python,environment-variables,voice,vonage From: 78796142