Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Unanswered
heavy-metal-blues-code asked this question in Q&A
Discussion options

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!

You must be logged in to vote

Replies: 2 comments 2 replies

Comment options

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

You must be logged in to vote
0 replies
Comment options

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.

You must be logged in to vote
2 replies
Comment options

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

Comment options

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /