【发布时间】:2021-05-18 03:59:57
【问题描述】:
我使用以下代码获取 MS Graph API 访问令牌:
# Resource Owner Password Credentials (ROPC)
def ropc_flow_session(AUTHORITY, RESOURCE, username, password, CLIENT_ID):
context = adal.AuthenticationContext(AUTHORITY)
token_response = context.acquire_token_with_username_password(RESOURCE, username, password, CLIENT_ID)
session = requests.Session()
session.headers.update({'Authorization': f'Bearer {token_response["accessToken"]}'})
return session
RESOURCE 和 AUTHORITY 值为:
RESOURCE = 'https://graph.microsoft.com'
AUTHORITY = F"https://login.microsoftonline.com/{tenant}"
当我调用ropc_flow_session 函数时,它会返回包含以下标头的会话:
{'User-Agent': 'python-requests/2.22.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Authorization': 'Bearer {ACCESS_TOKEN_VALUE}'}
所以我的理解是我可以访问已注册的应用程序。
当我尝试使用https://graph.microsoft.com/v1.0/me 调用me 时,错误消息显示:l
{'error': {'code': 'Authorization_RequestDenied',
'message': 'Insufficient privileges to complete the operation.',
我试图通过https://graph.microsoft.com/v1.0/sites/{TENANT}:/sites/{SITE_NAME}:/drives 访问用户是所有者的共享库,它提出了:
{'error': {'code': 'accessDenied',
'message': 'Access denied. You do not have permission to perform this action or access this resource.',
这是已注册的 App API 权限列表:
根据错误消息,我似乎已作为应用程序连接,但我使用了 ROPC 并传递了特定用户的用户名和密码。 任何想法表示赞赏
【问题讨论】:
-
你检查过jwt.ms中的访问令牌吗?
-
尝试使用范围
https://graph.microsoft.com/.default获取accessToken,看看是否有效。 -
是的,只是为了确保获得令牌中的所有权限。在 python 上不确定,但有一个范围参数可以获取所有权限。请参阅此document。试试看是否可以将其作为带有
https://graph.microsoft.com/.default值的参数发送。 -
您解码的访问令牌似乎不包含权限。请尝试执行“Grant admin consend for xxx”,然后使用访问令牌请求图形 api。
-
@Amir Maleki,如果您使用服务帐户添加权限,您将能够获得结果:)-
标签: python azure-active-directory microsoft-graph-api sharepoint-online onedrive