Auth0 Home Blog Docs

Python SDK: users.get() returning "400: Bad Request"

python

#1

Python SDK 3.2.2

Using client credentials to authenticate a python script that will update user profile attributes. Authentication is working fine, and mgmt API access is working fine in general. tenants.get(), clients.get() return the expected results, but users.get() returns 400: Bad Request. The client is authorized for read:users*.

from os import environ as env, path
from dotenv import load_dotenv
from auth0.v3.authentication import GetToken
from auth0.v3.management import Auth0
from auth0.v3.exceptions import Auth0Error

import constants

load_dotenv(path.join(path.dirname(__file__), ".env"))
AUTH0_CALLBACK_URL = env[constants.AUTH0_CALLBACK_URL]
AUTH0_CLIENT_ID = env[constants.AUTH0_CLIENT_ID]
AUTH0_CLIENT_SECRET = env[constants.AUTH0_CLIENT_SECRET]
AUTH0_DOMAIN = env[constants.AUTH0_DOMAIN]
MGMT_API_URL = 'https://'+AUTH0_DOMAIN+'/api/v2/'

def main():
    """main"""
    get_token = GetToken(AUTH0_DOMAIN)
    token = get_token.client_credentials(AUTH0_CLIENT_ID, AUTH0_CLIENT_SECRET, MGMT_API_URL)
    mgmt_api_token = token['access_token']

    auth0 = Auth0(AUTH0_DOMAIN, mgmt_api_token)

    print(auth0.tenants.get())         # THIS WORKS

    with open('user_ids.txt', 'r') as user_ids:
        for user_id in user_ids:
            try:
                user = auth0.users.get(user_id)        # 400: Bad Request
            except Auth0Error as auth0error:
                print(auth0error)

#2

Auth0 support solved the problem: trailing \n characters on the user_id values. Workaround:

user = auth0.users.get(user_id[:-1])

#3

The rstrip function is very good here:
user = auth0.users.get(user_id.rstrip())
This trims trailing whitespace including newlines, but will leave the string alone if there are no characters to trim.
Using [:-1] will cause bad results if a string comes through without a newline.

John