I am a newbie to Python and learning the best I can but I’m also new to using the GraphSDK (much more familiar with the Okta SDK – I am looking to just basically auth to Entra using an app registration which is working but it fails to do the user lookup and Im not sure why. The error I get is 2024-05-10 09:47:13,899 – ERROR – Error finding user: ‘GraphServiceClient’ object has no attribute ‘get’ but I am not sure what the best way to find the USERID then if there is no get method
Anyone have any ideas? I’m sure it’s something obvious.
import csv
import requests
import json
import subprocess
import urllib.parse
import logging
from msal import ConfidentialClientApplication
from msgraph import GraphServiceClient
from azure.identity.aio import ClientSecretCredential
# Setup basic logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
credentials = ClientSecretCredential(tenant_id, client_id, client_secret)
scopes = ['https://graph.microsoft.com/.default']
graph_client = GraphServiceClient(credentials=credentials, scopes=scopes)
def find_user_by_username(graph_client, username):
try:
request_url = f'/users?$filter=userPrincipalName eq '{username}''
result = graph_client.get(request_url) # Hypothetically assuming there's a 'get' method that can take a constructed URL
if result and 'value' in result and len(result['value']) > 0:
return result['value'][0].get('id')
except Exception as e:
logging.error(f"Error finding user: {e}")
return None
def update_user_in_ms_graph_stepwise(graph_client, username, intermediate_domain, new_username, immutable_id):
user_id = find_user_by_username(graph_client, username)
if user_id is None:
logging.error("User not found")
return "User not found"
try:
# Step 1: Use SDK methods to find the user by username and get their ID
search_result = graph_client.users.list(filter=f"userPrincipalName eq '{username}'")
users = list(search_result)
if not users:
return "User not found"
user_id = users[0].id
# Step 2: Update the username to the intermediate username
intermediate_username = f"{username.split('@')[0]}@{intermediate_domain}"
graph_client.users[user_id].update({
'userPrincipalName': intermediate_username
})
# Step 3: Set the immutable ID
graph_client.users[user_id].update({
'onPremisesImmutableId': immutable_id
})
# Step 4: Update to the final username
graph_client.users[user_id].update({
'userPrincipalName': new_username
})
logging.info(f"Found user ID: {user_id}")
return "User updated successfully to final username"
except Exception as e:
logging.error(f"An error occurred: {e}")
return str(e)
# Call the function
if __name__ == "__main__":
update_msg = update_user_in_ms_graph_stepwise(graph_client, username, intermediate_domain, new_username, immutable_id)
logging.info(update_msg)
I’ve tried just formatting it different ways and checking the SDK documentation but I think I’m overlooking something simple. Ran it through chatGPT but it lacks the updated SDK documentation so no help there. Help a poor noob 🙁
Steven Yurgelevic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.