我正在使用https://developer.microsoft.com/en-us/graph/graph-explorer发出请求
我正在尝试将它们转换为Python,以便用于一般自动化。
我总是从browser〉postman〉code中复制,所以我有我需要的所有cookie/token/etc,并且我的python请求会一直工作到某个东西过期。在这种情况下,那个东西是一个不记名令牌。
我想不出如何获得一个新的,有效的不记名令牌,除了重做上面的过程或复制令牌和复制粘贴到我的代码。
当我试图找到一个可以吐出一个的auth请求时,我在这里遇到了一个Postman的集合:
https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow
并且当我用我的orgs tenant_id替换{{tenant}}时,我得到一个带有 a 不记名令牌的200请求,但是当我将此不记名令牌插入到我的Graph API请求代码中时,我得到以下错误:
{"error":{"code":"BadRequest","message":"/me request is only valid with delegated authentication flow.","innerError":{"date":"2022-10-23T14:31:22","request-id":"...","client-request-id":"..."}}}
下面是Postman Auth的屏幕截图
下面是我的Graph API调用,它只适用于从graph-explorer复制的不记名令牌
def recreate_graph_request1(bearer = None):
'''
I went to https://developer.microsoft.com/en-us/graph/graph-explorer
and picked a request. Outlook>GET emails from a user
at first response was for some generic user, but I logged in using my account and it actually worked.
Then I used my old copy curl as bash trick to make it python
:return:
'''
url = "https://graph.microsoft.com/v1.0/me/messages?$filter=(from/emailAddress/address)%20eq%20%27my.boss@company.com%27"
payload = {}
headers = {
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.9',
'Authorization': bearer,
'Connection': 'keep-alive',
'Origin': 'https://developer.microsoft.com',
'Referer': 'https://developer.microsoft.com/',
'SdkVersion': 'GraphExplorer/4.0, graph-js/3.0.2 (featureUsage=6)',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-site',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
'client-request-id': 'n0t_th3_s4m3_4s_1n_P05tm4n',
'sec-ch-ua': '"Chromium";v="106", "Google Chrome";v="106", "Not;A=Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"'
}
response = requests.request("GET", url, headers=headers, data=payload)
return response
token_from_ms_auth = 'eyCOPIED_FROM_POSTMAN....'
bearer_from_ms_auth = 'Bearer '+token_from_ms_auth
print(recreate_graph_request1(bearer_from_ms_auth).text)
顺便说一句,我并不太乐观,任何不记名代币都能起作用,即使它与我的租户有某种联系--但我希望它能起作用,而随之而来的失望驱使我向宇宙寻求帮助。我不明白这些蜿蜒的流动,看别人的答案只会让我更加困惑。我希望有人能帮助我弄清楚这种情况。
1条答案
按热度按时间gudnpqoy1#
访问令牌的有效期很短。请在过期后刷新它们以继续访问资源。
请参考以下文档:https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow#refresh-the-access-token
希望这对你有帮助。