Help with sample Python code

Hi Everyone,

I’m experiencing a bit of trouble, and wondering if someone could shed light on what I’m doing wrong.

I want to list all our user accounts individually, on a separate line, like this:

Found User Account: bobjackson@company.org
Found User Account: janedoe@company.org
Found User Account: johnsmith@company.org

With the Python Requests library, I managed to page through the JSON results and get all users in an array. Fine. But what I’m having difficulties with is printing out the email of each individual user.

These are the results I’m getting:

Found User Account: [‘bobjackson@company.org’, ‘janedoe@company.org’, ‘johnsmith@company.org’]
Found User Account: [‘kensmith@company.org’,‘loisjackson@company.org’,‘marysmith@company.org’]
Found User Account: [‘nancydoe@company.org’,‘oscarmartinez@company.org’,‘paulmorgan@company.org’]

This is the Python code I’m using:

import requests

url_address = "https://api.zoom.us/v2/users"
headers = {
    "Authorization": "Bearer " + "MY_JWT_TOKEN",
    }

# find out total number of pages
r = requests.get(url=url_address, headers=headers).json()
total_pages = int(r['page_count']) + 1

# results will be appended to this list
all_entries = []

# loop through all pages and return JSON object
for page in range(1, total_pages):

    url = url_address + "?page_number=" + str(page)
    user_data = requests.get(url=url, headers=headers).json()
    user_ids = [(user['email']) for user in user_data['users']]
    all_entries.append(user_ids)
    page += 1

data = list(all_entries)

for email in data:
    print('Found User Account: {}'.format(email))

Any help would be GREATLY appreciated. Many thanks in advance!

Hi @rrodrigues,

Can you attach the API response that you get?

Thanks

1 Like

What’s the fastest way to send you that info? Postman?

I was able to fix the issue by changing a few lines of code in my FOR loop:

  1. Changed all_entries.append(user_ids) to all_entries.extend(user_ids)
  2. Then I changed data = list(all_entries), which was useless, to data = all_entries, and placed it above the page += 1 counter.

New working code below:

 # loop through all pages and return JSON object
 for page in range(1, total_pages):
     url = url_address + "?page_number=" + str(page)
     user_data = requests.get(url=url, headers=headers).json()
     user_ids = [(user['email']) for user in user_data['users']]
     all_entries.extend(user_ids)
     data = all_entries
     page += 1
 
 for email in data:
     print('Found User Account: {} '.format(email))
1 Like

Hey @rrodrigues,

Happy to hear you figured out the issue! :slight_smile:

Thanks,
Tommy