Error in GET request for Central API

Just to give you something to start from, here's a quick script that lists projects and app_users.

#!/usr/bin/env python3

import requests
import json

central_url = "https://central.example.org"
central_email = "myemail@email.com"
central_password = "mypassword"


def get_email_token():

    email_token_response = requests.post(
        central_url + "/v1/sessions",
        data=json.dumps({"email": central_email, "password": central_password}),
        headers={"Content-Type": "application/json"},
    )

    if email_token_response.status_code == 200:
        return email_token_response.json()["token"]


def list_app_users(email_token, central_project_id):

    app_users_response = requests.get(
        central_url + "/v1/projects/" + str(central_project_id) + "/app-users",
        headers={"Authorization": "Bearer " + email_token},
    )

    app_users = {}
    for app_user in app_users_response.json():
        app_users[app_user["id"]] = app_user["displayName"]
    return app_users


def list_projects(email_token):

    projects_response = requests.get(
        central_url + "/v1/projects/",
        headers={"Authorization": "Bearer " + email_token},
    )

    projects = {}
    for project in projects_response.json():
        projects[project["id"]] = project["name"]

    return projects


email_token = get_email_token()
if email_token:
    projects = list_projects(email_token)
    for project_id, project_name in projects.items():
        print("- Project", project_id, "- " + project_name)
        app_users = list_app_users(email_token, project_id)
        for app_user_id, app_user_name in app_users.items():
            print("  - App user", app_user_id, "- " + app_user_name)
else:
    print("Error getting email token")
6 Likes