无法通过 sinch 发送短信

问题描述 投票:0回答:2

我想通过 sinch 发送短信。我从 Sinch 网站注册并获得了密钥和秘密。但是我发送消息的代码不起作用。

import time
from time import sleep
from sinchsms import SinchSMS

# function for sending SMS
def sendSMS():

    # enter all the details
    # get app_key and app_secret by registering
    # a app on sinchSMS
    number = '+91XXXXXXXXX'
    app_key = 'KEY_ID'
    app_secret = 'Key_secret'

    # enter the message to be sent
    message = 'Hello Message!!!'

    client = SinchSMS(app_key, app_secret)
    print("Sending '%s' to %s" % (message, number))

    response = client.send_message(number, message)
    message_id = response['messageId']
    response = client.check_status(message_id)

    # keep trying unless the status returned is Successful
    while response['status'] != 'Successful':
        print(response['status'])
        time.sleep(1)
        response = client.check_status(message_id)

    print(response['status'])

if __name__ == "__main__":
    sendSMS()

我试过有和没有国家代码。我收到以下错误

Sending 'Hello Message!!!' to +91XXXXXXXXXX
---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
<ipython-input-47-97ce28144276> in <cell line: 35>()
     34 
     35 if __name__ == "__main__":
---> 36         sendSMS()

8 frames
/usr/lib/python3.10/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs)
    641 class HTTPDefaultErrorHandler(BaseHandler):
    642     def http_error_default(self, req, fp, code, msg, hdrs):
--> 643         raise HTTPError(req.full_url, code, msg, hdrs, fp)
    644 
    645 class HTTPRedirectHandler(BaseHandler):

HTTPError: HTTP Error 401: Unauthorized
python sms sinch
2个回答
0
投票

这实际上是我的私人帐户,但我为 Sinch 工作,在我们的团队中,我们有一个使用 Sinch SMS API 的应用程序。我们的客户每天通过 Sinch SMS 发送数千条 SMS 消息。

你得到的错误:

HTTPError: HTTP Error 401: Unauthorized

因此很可能您的 app_keyapp_secret 不正确。 我的建议是从仪表板仔细检查这些。我认为这是一个很好的文档:https://developers.sinch.com/docs/sms/getting-started/python/python-send-sms/

旁注 1:我看到您正在使用 Python 并使用来自 https://github.com/sinch/python-sinch-sms 的 SinchSMS 助手。如果您想减少依赖性,没有这个额外的帮助程序(没有添加更多代码行)也很容易工作。

Sidenote 2 关于国家代码:根据我的经验,通过 Sinch SMS 发送 SMS 时,您应该始终包括前导加号。在我的测试中,它在没有加号的情况下也能正常工作,但是如果收件人回复,发件人号码将根据 E.164 并包括加号。所以,如果在你的应用程序中你想匹配传出和传入的消息(可能为用户创建类似聊天的链接体验),那么当你始终使用 E.164 时会更容易一些。


0
投票

您是否使用了正确的 Python 版本并安装了 OpenSSL?出于好奇,我尝试了这个并且现在正在使用 Mac M1。当我尝试时,似乎需要 OpenSSL,并且它仅在 Python 3.11 之后有效:

我安装 Pyenv(Python 的 nvm 版本):

酿造安装 pyenv

然后版本3.11.3为了安装OpenSSL:

pyenv 安装 3.11.3

并且,为了使用 Python 版本 3.11.3:

pyenv 全球 3.11.3

接下来,为了安装OpenSSL:

酿造安装 [email protected]

最后,Sinch 本身:

pip 安装 sinch

在这里,我使用并正确运行的文档中的示例代码,我收到了 SMS(请参阅评论):

from sinch import Client

# Here, where I created my credentials:
# https://dashboard.sinch.com/settings/access-keys

sinch_client = Client(
    key_id="YOUR_key_id",
    key_secret="YOUR_key_secret",
    project_id="YOUR_project_id"
)

# And here where you can find your number:
# https://dashboard.sinch.com/sms/start/api-integration

send_batch_response = sinch_client.sms.batches.send(
    body="Hello from Sinch!",
    to=["YOUR_to_number"],
    from_="YOUR_Sinch_number",
    delivery_report="none"
)

print(send_batch_response)

顺便说一下,我不是 100% 确定这是否是您的答案。我使用了这个例子,因为你缺少一些值,比如“YOUR_Sinch_number”或“project_id”。

© www.soinside.com 2019 - 2024. All rights reserved.