Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Azure Graph: Access Token missing or malformed

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.
like image 282
Jakob Kristensen Avatar asked Apr 18 '18 09:04

Jakob Kristensen


1 Answers

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!

like image 86
Wayne Yang Avatar answered Nov 18 '22 13:11

Wayne Yang