Get Additional UserData

I am trying to figure out how to add to this current script below to add two fields. One i know exists and not sure on the other. Group Name and Last Date Logged in.

I just can't figure out the syntax. If anyone could help.. .would be appreciated. I took out the authentication part because I am not having an issue connecting.. .its purely the syntax of the fields i am trying to bring in. If Last Date Logged in isn't available that is fine... then i just need to add the group name to the data.

import requests
import base64
import time

def getUserList():
file = open(fileName, 'w+')
file.write('Name:GUID:Username:E ID:Team ID:DivisionID:GroupID\n')
userCount = 0
url = '/api/v2/users'
usDict = {}
jD = tokenRequest()
ahs2 = {"Authorization": "{} {}".format(jD['token_type'], jD['access_token']), "Content-Type": "application/json"}
sN = 1
resp2 = requests.get("{}{}?pageSize=20000&pageNumber={}&expand=employerInfo%2Cbiography".format(aBase, url, sN),
                     headers=ahs2)
pN = resp2.json()['pageCount'] + 1
while sN < pN:
    resp2 = requests.get("{}{}?pageSize=20000&pageNumber={}&expand=employerInfo%2Cbiography".format(aBase, url, sN),
                         headers=ahs2)
    data = resp2.json()['entities']

    for x in data:
        usLg = x['username']
        usNm = x['name']
        usId = x['id']
        usDivisionID = x['division']['id']



        if 'biography' in x['biography']:
            usBg = x['biography']['biography']
        else:
            usBg = "NO TEAM ID"

        if 'employerInfo' in x:
            if 'employeeId' in x['employerInfo']:
                usEd = x['employerInfo']['employeeId']
            else:
                usEd = 'NO EID'
        else:
            usEd = 'NO EID'


        userCount = userCount + 1
        file.write('{}:{}:{}:{}:{}:{}\n'.format(usNm, usId, usLg, usEd, usBg, usDivisionID))
    sN = sN + 1
enTm = time.time()
toTm = float((enTm - stTm) / 60)
print('\n Completed {} users in {} minutes!'.format(userCount, toTm))


# Set script variables here

tBase = "https://login.usw2.pure.cloud"
aBase = "https://api.usw2.pure.cloud"
stTm = time.time()

# set File name here
fileName = 'UQL-tabbed.txt'

getUserList()

Hi Dean,

I'd just like to ask: Is there a particular reason for doing the API calls using only requests? We do recommend using the Python SDK as it can help better with development with the models and the methods for the API calls.

As for the question itself, you can add 'groups' to your 'expand' query parameter so it will return the groups where the user is a member of. It will just consist of group ids, so you'll subsequently need to GET group using the id to get the details like the name or cross reference it if you've already got a list of all the groups.

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.