cancel
Showing results for 
Search instead for 
Did you mean: 

REST API and Python

Silutions
10 - ETL
10 - ETL

i'm a Python newbie looking for more REST API script samples using Python.  For example, I'm trying to export a simple user list with email, firstName and lastName with a certain search value using the 0.9 GetUsers endpoint.  I have tested this in the Swagger interface and get the correct results.  I'm trying to use Python to access the data without success.  My code looks like this:

import requests
import json

headers = {
'X-XSRF-TOKEN':'eyJhbGciOiJIUzI1NiIsInxxxxxxxxxxx0ZW5hbnRJZCI6IjYxYjExNmI2OTdiYTBiMDAxYTk2MTgxMyIsImFsbG93ZWRUZW5hbnRzIjpbIjYxYjExNmI2OTdiYTBiMDAxYTk2MTgxMyJdLCJjc3JmVG9rZW4iOiJIaE1yWldBRzBlRUVncG9MRm5VNldlL0J3dEQ4cWJPRkR4azZpdThpVUdzNUh2ZW1haGh3dEJianBsaUYvY0dSIiwiZXhwIjoxNjQ0OTQ5NjM0LCJpYXQiOjE2NDQzNDQ4MzR9.9tWIp0XDzEtZu9Q0WTtSrzB8uGRZM0QFsar7lqZE1wE',
'accept': 'application/json'
}

url = 'https://xxxxx.yyyyy.com/api/v1/users?search=ZZZ&fields=email%2CfirstName%2ClastName'

response = requests.get(url, headers=headers)

print(json.loads(response.text))

This results in a 401 unauthorized error.  I have a feeling something is not right with the header since I did create a user token.

Any assistance will be greatly appreciated.  Note:  This is not really a Notebooks topic, but it was the closest label I spotted.

Regards, Jim

1 ACCEPTED SOLUTION

harikm007
13 - Data Warehouse
13 - Data Warehouse

@Silutions ,

Please check this python script:

import requests
import json

username = 'your username'
password = 'your password'
server_name = 'https://XXXXX.YYYYY.com'

login_data =    {  'username' : username,
                        'password' : password
                    }

login_url = server_name + '/api/v1/authentication/login'
login_res = requests.post(url=login_url, data=login_data).json()

access_token = login_res.get('access_token')
if len(access_token) > 0:
    print('login succesful.')
    
api_header = {'Authorization': 'Bearer ' + access_token}

url = server_name + '/api/v1/users?search=ZZZZ&fields=email%2CfirstName%2ClastName'

response = requests.get(url, headers=api_header)

print(json.loads(response.text))

-Hari

View solution in original post

2 REPLIES 2

harikm007
13 - Data Warehouse
13 - Data Warehouse

@Silutions ,

Please check this python script:

import requests
import json

username = 'your username'
password = 'your password'
server_name = 'https://XXXXX.YYYYY.com'

login_data =    {  'username' : username,
                        'password' : password
                    }

login_url = server_name + '/api/v1/authentication/login'
login_res = requests.post(url=login_url, data=login_data).json()

access_token = login_res.get('access_token')
if len(access_token) > 0:
    print('login succesful.')
    
api_header = {'Authorization': 'Bearer ' + access_token}

url = server_name + '/api/v1/users?search=ZZZZ&fields=email%2CfirstName%2ClastName'

response = requests.get(url, headers=api_header)

print(json.loads(response.text))

-Hari

Hari - worked like champ.  Thank you! - Jim