Applied Value Group: Knowledge Management System September 2023

This is my monthly dev-log on the Applied Value Kknowledge Management System.

Meeting 2023-09-12

Authorization

Authentication has been a sticking point earlier this month. Box makes it exceptionally difficult to set authorization settings. What I've learned so far...

Authenticating the BoxSDK Client (Python)

from boxsdk import Client, JWTAuth

auth = JWTAuth.from_settings_file(JWT_JSON_TOKEN_FROM_DEV_CONSOLE.json)
auth.authenticate_instance()
client = Client(auth)

client = self.get_client()
try:
# user = client.user(user_id=user_id).get()
user = client.user(user_id=user_id)
client = client.as_user(user)
username = client.user().get().name
print(f"Debug: Client should now act as user {username}")
except Exception as e:
print(f"Failed to authenticate as user {user_id}: {e}")

folder = self.client.folder(folder_id=dir_id)
items = [item for item in folder.get_items()]
print(items)

This code snippet above creates an auth object from JWTAuth from boxsdk. This will craete an auth object that has all of the authorization configurations of the JWT token created from the Box Dev Console. This can be used to create a Client object or client that can be used to perform Box SDK operations, like viewing the contents of a folder.

First to be able to perform these operations as a different user, you need to use the client authenticated with the JWTAuth, to then use the client.as_user(client.user(user_id=SOME_USER_ID)), which finally gets you an updated Client object that now performs SDK operations as that user with user ID SOME_USER_ID.

References

Web Links

Note Links