Newbie Assistance with Python Graph SDK and Updating Usernames

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 🙁

New contributor

Steven Yurgelevic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật