Generate token with Python
In this article, we will explain how you can generate an access token and how you can use it to interact with our API.
To get started, ensure that you have a free Sonetel account. You will have to use your username and password to acquire an access token. The following code shows you how to prepare a request payload with your username and password.
Note that we get your username and password from environment variables for security among the code below. You only need to modify the user and pswd variables.
import urllib.parse import requests import os user = os.environ.get('sonetelUserName') pswd = os.environ.get('sonetelPassword') url = "https://api.sonetel.com/SonetelAuth/oauth/token" payload = urllib.parse.urlencode({ 'grant_type': 'password', 'password': pswd, 'refresh':'yes', 'username': user }) headers = { 'Accept': 'application/json, text/plain', 'Content-Type': 'application/x-www-form-urlencoded' }
Next, we can finally send our request to fetch an access token.
response = requests.request( "POST", url, auth=('sonetel-api', 'sonetel-api'), data=payload, headers=headers ) response.raise_for_status() token = response.json() print(token)
If you want to refer to the full code in our GitHub repository, check this link. If you also want to find more about our APIs, check our API documents.
We learned how to generate an access token using Python. Once you get your Sonetel account, the rest should be straightforward.
Photo by Kelly Sikkema on Unsplash