Python Adal - 无法使用Access Token检索Outlook邮件

问题描述 投票:0回答:2

我最近一直在尝试使用Python库的Azure活动目录将电子邮件提取过程从Powershell移植到Python。我一直试图使用acquire_token_with_client_credentials函数来做到这一点,但我遇到了麻烦。

我可以使用下面的代码返回访问代码但我不能使用生成的令牌返回任何邮箱项目。

我已设法创建一个本机应用程序并使用acquire_token_with_username_password成功访问消息,但无法获取该组代码以在我的远程桌面上工作,因为它打印错误读取:

“由于管理员进行了配置更改,或者您搬到了新位置,因此必须使用多重身份验证”

我已经准备好客户端凭据流但仍然无法让下面的示例工作。谁能看到我哪里出错了?

def test8():

import adal
import requests

authority_url = "https://login.microsoftonline.com/"+lf_tenantid
context = adal.AuthenticationContext(
    authority_url,
    validate_authority=True,
    api_version=None
)

resource = 'https://outlook.office.com/'

token = context.acquire_token_with_client_credentials(
    resource=resource,
    client_id = etl_clientid2,
    client_secret = etl_clientsecret2
)

access_token = token['accessToken']

print(token)
#######################################NONE OF THIS PART WORKS
#######################################
#######################################

folder_id = etl_folderid
url = "https://outlook.office.com/api/v2.0/me/MailFolders/"+folder_id+"/messages"

headers = {
    'Authorization': 'Bearer '+access_token
}

r = requests.get(url, headers=headers)
print(r)
python-3.x client credentials adal http-status-code-401
2个回答
2
投票

对于任何感兴趣的人,这是我用来解决访问令牌问题的代码:

def save_windows_refreshtoken(app_name, client_id, client_secret):

#import adal
#import requests
import json
import pandas as pd


# OAuth endpoints given in Outlook API documentation
authorization_base_url = 'https://login.microsoftonline.com/common/oauth2/authorize'
token_url = 'https://login.microsoftonline.com/common/oauth2/token' #provides a refresh and access token

redirect_uri = "http://localhost:8000"

from requests_oauthlib import OAuth2Session
outlook = OAuth2Session(client_id,redirect_uri=redirect_uri)

# Redirect  the user owner to the OAuth provider (i.e. Outlook) using an URL with a few key OAuth parameters.
authorization_url, state = outlook.authorization_url(authorization_base_url)
print('Please go here and authorize,', authorization_url)

#The above redirects you to a localhost page (which is blank) but returns a string containing a code which can be used below
#rememebr the search for "&" because there's a couple of bits of data after the code that need to be deleted from the code string before it can be used

# Get the authorization verifier code from the callback url
redirect_response = input('Paste the full redirect URL here:')

# Fetch the access token
token = outlook.fetch_token(token_url,client_secret=client_secret,code=redirect_response)


#convert the returned token json into a dataframe
j_dump = json.dumps(token, sort_keys=True,indent=4, separators=(',', ': ')) #pull out the value data from the json file, messages are stored in value

df = pd.read_json(j_dump) #read the json file into a dataframe

first_row = df.iloc[0] #pull the first row so we can format a new table from it

d = {
    'app_name' : pd.Series([app_name]),
    'refresh_token' : pd.Series([first_row.refresh_token])
    }
data = pd.DataFrame(d)

0
投票

您正在使用acquire_token_with_username_password()获取消息,因为您通过用户凭据获取给定资源的令牌。

由于客户端凭据流用于反向通道(服务器到服务器通信),因此用户不参与其中并且您正在获取服务主体令牌。

我建议代表流或授权代码流(acquire_token_with_authorization_code)使用服务器应用程序的授权代码获取给定资源的令牌,并调用outlook api来读取消息。

以下是链接(Outlook Mail API和Python):

https://github.com/jasonjoh/python_tutorial/tree/outlook-api

Python adal库支持其他身份验证方法。以下是文档链接:https://adal-python.readthedocs.io/en/latest/

我们建议现在在Microsoft Graph中公开Office 365服务,如OneNote,Outlook,Excel,OneDrive,Microsoft Teams,Planner和SharePoint。

https://docs.microsoft.com/en-us/previous-versions/office/office-365-api/

© www.soinside.com 2019 - 2024. All rights reserved.