Unable to send sms from through sinch

374 Views Asked by At

I want to send sms through sinch. I registered and obtained key and secret from Sinch website. But my code to send message is not working.

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()

I tried with and without country code. I am getting the following error

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
2

There are 2 best solutions below

0
tnurmi On

This is actually my private account but I work for Sinch, and in our team we have an app which utilizes Sinch SMS API. Our customers send thousands of SMS messages through Sinch SMS on daily basis.

The error you are getting:

HTTPError: HTTP Error 401: Unauthorized

Therefore most likely either your app_key or app_secret is incorrect. My suggestion is to double check these from the Dashboard. I think this is a good document: https://developers.sinch.com/docs/sms/getting-started/python/python-send-sms/

Sidenote 1: I see you are on Python and using the SinchSMS helper from https://github.com/sinch/python-sinch-sms. It's also quite easy to work without this extra helper (not many more code lines added), if you want to reduce your dependencies.

Sidenote 2 about country code: It is my experience that you should always include the leading plus when sending out SMS through Sinch SMS. In my tests it worked also without the plus, however if the recipient replies, the sender number will be according to E.164 and includes the plus. So, if in your app you want to match the outgoing and incoming messages (to maybe create chat-like chained experience for user), it's a bit easier when you always use E.164.

0
osmancalisir On

Are you using the correct Python version and OpenSSL installed? Since you are getting 'HTTPError: HTTP Error 401: Unauthorized' error, it seems you already send the request and get a response. Maybe it's just a credential issue.

Here are two pages that you can create or see the credentials:

https://dashboard.sinch.com/settings/access-keys

https://dashboard.sinch.com/sms/start/api-integration

But if not, here how I run and get SMS, out of curiosity, I tried this and am using Mac M1 right now. When I try, it seems like OpenSSL is required and it only works after Python 3.11:

I install Pyenv (the nvm version of Python) with:

brew install pyenv

Then the version 3.11.3 in order to install OpenSSL:

pyenv install 3.11.3

And, in order to use Python version 3.11.3:

pyenv global 3.11.3

Next, in order to install OpenSSL:

brew install [email protected]

And lastly, the Sinch itself:

pip install sinch

And here, the example code from documentation that I used and run correctly, and I get the SMS(please see the comments):

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)

I am not 100% sure if this is your answer by the way. I used this example, because yours missing some values like 'YOUR_Sinch_number' or 'project_id'.