5

I am trying to access some information about users on an AD network through Azure Graph API. The code looks like this:

from azure.common.credentials import ServicePrincipalCredentials
from azure.graphrbac import GraphRbacManagementClient

TENANT = 'something.onmicrosoft.com'
TENANT_ID = '...'
CLIENT_ID = '...'
SECRET = '...'

credentials = ServicePrincipalCredentials(
    client_id=CLIENT_ID,
    secret=SECRET,
    tenant=TENANT,
)
client = GraphRbacManagementClient(credentials, TENANT_ID)

client.users.list().next()

credentials does not fail, but i get the following error anyway:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/ifs/home/.../.local/lib/python2.7/site-packages/msrest/paging.py", line 121, in __next__
    self.advance_page()
  File "/ifs/home/.../.local/lib/python2.7/site-packages/msrest/paging.py", line 107, in advance_page
    self._response = self._get_next(self.next_link)
  File "/ifs/home/.../.local/lib/python2.7/site-packages/azure/graphrbac/operations/users_operations.py", line 158, in internal_paging
    raise models.GraphErrorException(self._deserialize, response)
azure.graphrbac.models.graph_error.GraphErrorException: Access Token missing or malformed.

1 Answer 1

8

You missed resource in your code. Try to use following code:

from azure.common.credentials import ServicePrincipalCredentials
from azure.graphrbac import GraphRbacManagementClient

TENANT = 'something.onmicrosoft.com'
TENANT_ID = '...'
CLIENT_ID = '...'
SECRET = '...'

credentials = ServicePrincipalCredentials(
    client_id=CLIENT_ID,
    secret=SECRET,
    tenant=TENANT_ID,
    resource="https://graph.windows.net"
)
client = GraphRbacManagementClient(credentials, TENANT)

client.users.list().next()

You can also see more detials about using Azure Active Directory Graph Rbac API via Python in this doc.

Please let me know if it helps!

Sign up to request clarification or add additional context in comments.

1 Comment

This helps! Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.