Reputation: 302
I could manage to get access to Azure resources with the code bellow:
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.compute import ComputeManagementClient
from azure.graphrbac import GraphRbacManagementClient
subscription_id = "aaaa"
tenant_id = "bbbb"
credentials = ServicePrincipalCredentials(
client_id="cccc",
secret="dddd",
tenant=tenant_id
)
client = ResourceManagementClient(credentials, subscription_id)
for item in client.resource_groups.list():
print item
compute_client = ComputeManagementClient(credentials, subscription_id)
disks = compute_client.disks.list()
for disk in disks:
print disk
But I can't access Azure AD with the same code!!! Is there a different way to access it? Why is it different?! See the code bellow:
graphrbac_client = GraphRbacManagementClient(credentials, subscription_id)
for item in graphrbac_client.groups.list():
print item
Error:
GraphErrorExceptionTraceback (most recent call last) in () 1 graphrbac_client = GraphRbacManagementClient(credentials, subscription_id) 2 ----> 3 for item in graphrbac_client.groups.list(): 4 print item
/home/andre/.local/lib/python2.7/site-packages/msrest/paging.pyc in next(self) 129 return response 130 else: --> 131 self.advance_page() 132 return self.next() 133
/home/andre/.local/lib/python2.7/site-packages/msrest/paging.pyc in advance_page(self) 115 raise StopIteration("End of paging") 116 self._current_page_iter_index = 0 --> 117 self._response = self._get_next(self.next_link) 118 self._derserializer(self, self._response) 119 return self.current_page
/home/andre/.local/lib/python2.7/site-packages/azure/graphrbac/operations/groups_operations.pyc in internal_paging(next_link, raw) 336 337 if response.status_code not in [200]: --> 338 raise models.GraphErrorException(self._deserialize, response) 339 340 return response
GraphErrorException: Access Token missing or malformed.
azure-common version = 1.1.14
Upvotes: 2
Views: 5300
Reputation: 24549
Access Token missing or malformed.
ComputeManagementClient
resource path is https://management.azure.com
But for GraphRbacManagementClient the resource path is https://graph.windows.net
. So you got the exception.
How to access Azure AD with Python SDK?
You could get the answer from this link. The following code is the snippet from the document.
from azure.graphrbac import GraphRbacManagementClient
from azure.common.credentials import UserPassCredentials
# See above for details on creating different types of AAD credentials
credentials = UserPassCredentials(
'[email protected]', # Your user
'my_password', # Your password
resource="https://graph.windows.net"
)
tenant_id = "myad.onmicrosoft.com"
graphrbac_client = GraphRbacManagementClient(
credentials,
tenant_id
)
Upvotes: 1