Python Azure图: Access Token missing or malformed

标签 python azure azure-active-directory azure-ad-graph-api

我正在尝试通过 Azure Graph API 访问有关 AD 网络上用户的一些信息。代码如下所示:

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()

凭据不会失败,但我仍然收到以下错误:

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.

最佳答案

您在代码中错过了资源。尝试使用以下代码:

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()

您还可以在 this doc 中查看有关通过 Python 使用 Azure Active Directory Graph Rbac API 的更多详细信息。 .

如果有帮助请告诉我!

关于Python Azure图: Access Token missing or malformed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49895598/

相关文章:

azure - 我们的 MS Graph Webhook 未收到删除 AAD 中用户的请求

oauth-2.0 - 使用 oAuth 和 SAML 代表 Azure AD

python - configparser.NoSectionError : No section: 'myvars'

python - Pytorch:如何生成长度在一定范围内的随机向量?

python - 如何在不使用 dir() 的情况下获取 xmlrpclib.ServerProxy() 的方法列表?

azure - 我的 Azure Synapse 笔记本已成功完成,但收到错误 "Error code 1 EXCEPTION_DURING_SPARK_JOB_CLEANUP"

node.js - 使用 azure 函数 nodejs 创建缩略图

azure - Databricks:无法访问已安装的 blob

azure - 大容量复制程序 (bcp) 实用程序 - 导入 CSV 时出错 - 证书链由不受信任的机构颁发

Python OpenCV 颜色跟踪