-
Notifications
You must be signed in to change notification settings - Fork 48
The class GraphClient is no longer available in the latest msgraph-core PyPI package. Seeking Alternatives for Microsoft Graph API App-only Authentication in Python
#462
-
In my project, I've been utilizing the following code snippet to initialize the Microsoft Graph client with app-only authentication:
def ensure_graph_for_app_only_auth(self):
#Ensure that the class is set up for app-only authentication using a client secret.
#Initializes client credentials and the app client if not already set.
if not hasattr(self, 'client_credential'):
client_id = self.settings['clientId']
tenant_id = self.settings['tenantId']
client_secret = self.settings['clientSecret']
self.client_credential = ClientSecretCredential(tenant_id, client_id, client_secret)
if not hasattr(self, 'app_client'):
self.app_client = GraphClient(credential=self.client_credential,
scopes=['https://graph.microsoft.com/.default'])
I'm reaching out to the community to explore alternative approaches or libraries for authenticating with the Microsoft Graph API in Python. If anyone has suggestions, recommendations, or knows of alternative libraries that offer a similar functionality for app-only authentication with the Microsoft Graph API, your insights would be immensely valuable. I use this method primarily to retrieve members of a Microsoft Entra ID group as you can see in following method implementation.
def get_groupmembers(self, gid):
"""
Retrieves members of a group specified by its ID.
Args:
gid (str): ID of the group.
Returns:
dict: Dictionary containing details of group members.
"""
self.ensure_graph_for_app_only_auth()
members_endpoint = f'/groups/{gid}/members'
members_response = self.app_client.get(members_endpoint)
all_members = []
while members_response:
members = members_response.json().get('value', [])
all_members.extend(members)
next_link = members_response.json().get('@odata.nextLink')
if next_link:
members_response = self.app_client.get(next_link)
else:
break
return {"value": all_members}
Additionally, I'd appreciate advice on how to stay informed about future changes to the msgraph-core package. Specifically, I want to avoid issues arising from such as the retirement of the GraphClient` that impacted my production environment.
Is there a recommended source or channel to follow for timely updates on such changes?
Thank you all in advance for your expertise and assistance!
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments 2 replies
-
I made a swift operation and now to workaround this issue I have hard-coded a previous version of the msgraph-core package using a setup.py
Beta Was this translation helpful? Give feedback.
All reactions
-
The examples at https://github.com/microsoftgraph/msgraph-sdk-python/blob/main/docs/authentication_samples.md or https://github.com/microsoftgraph/msgraph-sdk-python cover your question about the app-only authentication.
However you will need to modify get_groupmembers() according to the examples here and either use paging or pass the next_link like this app_client.groups.with_url(next_link).get() if that method is available for groups. For users it is available.
Beta Was this translation helpful? Give feedback.
All reactions
-
Thank you for your detailed response.
I've decided to use a direct requests call with the Azure Graph API. Therefore, I will be relying on the Python requests package and the msal package, allowing me to eliminate the need for the msgraph-core package
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
For anyone else stumbling upon this azure.identity also provides a convenient way to create credentials and tokens that can be used in requests directly.
Sadly this serves as another point of evidence that SDKs that try to provide a "convenient abstraction" over existing REST APIs are often more hassle than they are worth. Broken endpoints, broken documentation, out of date examples. Just use HTTP requests.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1