- Access exclusive content
- Connect with peers
- Share your expertise
- Find support resources
05-01-2019 10:44 AM - last edited on 09-02-2020 10:51 AM by CHopson
Hi- I'm trying to use the API with Python and am having some trouble getting authorized to obtain data using the x-redlock-auth request header.
First, let me add the disclaimer that I'm no Python expert so the help is appreciated even if it seems basic. I also understand that PAN/ Redlock isn't here to teach us how to code but I'm looking for an API doc example at a minimum which seems reasonable.
BTW: the API documentation is great and it's cool that there are code examples!
That said, I'm left wondering about using the Python requests module and how to state the x-redlock-auth mandatory request header.
The docs say that the x-redlock-auth and Content-Type are mandatory. I can successfully declare declare content type as application/json using standard Python response module code.
I'm also able to get my JWT andI can define the content type ok as I can get my JWT and parse into a variable from the successful JSON response.
However, I don't see example code to use x-redlock-auth in the request header.
In the examples there is a pop-up for x-redlock-auth prompting for the JWT but the code example doesn't refelct how to use in the params statement of the requests module.
Example code snippet from the REST API Doc's - generated using the the query params.
Can anyone provide an example of defining the headers for the required x-redlock-auth mandatory request headers in the context of this?
Thanks for any help! Would love to automate Redlock login monitoring into a security validation workflow at my organization.
05-08-2019 04:58 AM
This is how I'm logging in to the API and setting up headers for subsequent requests using Python.
05-08-2019 08:55 PM
A similar alternative to the above:
import requests import json def login() header = {'Content-Type':'application/json'} payload = {'username':'redlock_user','password':'redlock_pw','customerName':'customer_name'} API = 'https://api.redlock.io' response = requests.request('POST', '{}/login'.format(API), json=payload, headers=header) json_response = response.json() return json_response['token'] JWT_TOKEN = login() REQ_HEADER = {'Content-Type':'application/json','x-redlock-auth':JWT_TOKEN} # Example data pull response = requests.request('GET', '{}/cloud'.format(API), headers=REQ_HEADER) cloud_accounts = json.loads(response.text)
That small code snippet should load all cloud account instances in RedLock and save them to an array cloud_accounts.
Hope this helps.
05-08-2019 04:58 AM
This is how I'm logging in to the API and setting up headers for subsequent requests using Python.
05-08-2019 08:55 PM
A similar alternative to the above:
import requests import json def login() header = {'Content-Type':'application/json'} payload = {'username':'redlock_user','password':'redlock_pw','customerName':'customer_name'} API = 'https://api.redlock.io' response = requests.request('POST', '{}/login'.format(API), json=payload, headers=header) json_response = response.json() return json_response['token'] JWT_TOKEN = login() REQ_HEADER = {'Content-Type':'application/json','x-redlock-auth':JWT_TOKEN} # Example data pull response = requests.request('GET', '{}/cloud'.format(API), headers=REQ_HEADER) cloud_accounts = json.loads(response.text)
That small code snippet should load all cloud account instances in RedLock and save them to an array cloud_accounts.
Hope this helps.
05-08-2019 09:18 PM
Thanks for the response. I didn't get to testing for my use case but looks to make sense with the headers definition for 'x-redlock-auth' being passed to reqests. Will mark as a solution when I get to testing.
05-08-2019 09:19 PM
Thanks for another approach. I didn't get to testing for my use case but looks to make sense with the headers definition for 'x-redlock-auth' being passed to reqests. Will mark as a solution when I get to testing.
05-13-2019 10:02 AM
06-03-2020 04:05 PM
Where do you get the value of "CustomerName" in the request to get the JWT?
12-12-2022 11:50 PM
the "customername" parameter is optional.
def login():
header = {'Content-Type':'application/json'}
payload = {'username':'Access ID','password':'Secret Key'}
response = requests.request('POST', '{}/login'.format(API), json=payload, headers=header)
json_response = response.json()
return json_response['token']
JWT_TOKEN = login()
Click Accept as Solution to acknowledge that the answer to your question has been provided.
The button appears next to the replies on topics you’ve started. The member who gave the solution and all future visitors to this topic will appreciate it!
These simple actions take just seconds of your time, but go a long way in showing appreciation for community members and the LIVEcommunity as a whole!
The LIVEcommunity thanks you for your participation!