macadmins / jamf-pro-sdk-python

A client library for the Jamf Pro APIs and webhooks.
https://macadmins.github.io/jamf-pro-sdk-python/
MIT License
48 stars 10 forks source link

[Bug] ApiClientCredentialsProvider -> AccessToken() -> Pedantic -> Scope #31

Closed macnotes closed 11 months ago

macnotes commented 11 months ago

Feel free to tell me to go figure it out for myself but I though I'd check in with you first in case you were just still working on this part. It's still alpha, after all.

I'm getting an error trying ApiClientCredentialsProvider. from jamf_pro_sdk import ApiClientCredentialsProvider didn't work but it does if I add it to the from .clients.auth import list in ./jamf_pro_sdk/__init__.py or if I import it by path... from jamf_pro_sdk.clients.auth import ApiClientCredentialsProvider

I tried using it and get an error where pydantic is expecting a scope that doesn't yet exist.

  File "/Users/admin/PycharmProjects/Jamf Client API/bryson.py", line 32, in <module>
    credentials=ApiClientCredentialsProvider(client_id=get_env_value("client_id"),
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/admin/PycharmProjects/Jamf Client API/venv/lib/python3.11/site-packages/jamf_pro_sdk/clients/auth.py", line 155, in __init__
    super().__init__()
  File "/Users/admin/PycharmProjects/Jamf Client API/venv/lib/python3.11/site-packages/jamf_pro_sdk/clients/auth.py", line 41, in __init__
    self._access_token = AccessToken()
                         ^^^^^^^^^^^^^
  File "/Users/admin/PycharmProjects/Jamf Client API/venv/lib/python3.11/site-packages/jamf_pro_sdk/models/__init__.py", line 9, in __init__
    super().__init__(**kwargs)
  File "/Users/admin/PycharmProjects/Jamf Client API/venv/lib/python3.11/site-packages/pydantic/main.py", line 164, in __init__
    __pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__)
pydantic_core._pydantic_core.ValidationError: 1 validation error for AccessToken
scope
  Field required [type=missing, input_value={}, input_type=dict]

This is what I was trying...

import logging
import os
from jamf_pro_sdk import JamfProClient
from jamf_pro_sdk.clients.auth import ApiClientCredentialsProvider
from jamf_pro_sdk.helpers import logger_quick_setup

logger_quick_setup(level=logging.DEBUG)

def get_env_value(variable_name):
    """
    :param variable_name:
    :return: value

    Throws env error if 1) env var does not exist or 2) it exists but has no value
    """
    value = os.getenv(variable_name)
    if not value:
        raise EnvironmentError(f"get_env_value: The {variable_name} variable exists but does not have a value")
    return value

credentials=ApiClientCredentialsProvider(client_id=get_env_value("client_id"),
                                         client_secret= get_env_value("client_secret"))

client = JamfProClient(
    server=get_env_value("server"),
    credentials=credentials
)

System Information

Python 3.11 Sonoma 14.1 Jamf Pro SDK 0.4a1

brysontyrrell commented 11 months ago

I'm not able to recreate the error you're seeing using your code.

The internal AccessToken model is throwing an error that a type is not being set (the SDK differentiates between user and client tokens). The only way I could see this happening is if you somehow had code for an older version of the SDK mixed with the latest code.

Can you try uninstalling the SDK package and reinstalling it cleanly and trying your code again?

macnotes commented 11 months ago

Clean venv fixed it. Thank you! I'm sorry I wasted your time.

I had used pip to update from an old version. Not sure how it got borked. 🤷‍♂️