我在尝试使用 Python 制作的 MS Teams 聊天机器人方面需要一些帮助或指导。
无论如何,这里有几个事实:我已经获得了一个在我公司的管理员处注册的 API 应用程序,并且我收到了一个 MS Teams 用户,该用户将与我们在 MS Teams 中的群聊(不是 Teams 组)进行交互对话,但只是普通的群聊)。我有租户 ID,我有客户端 ID 和密钥,应用程序具有 MS 图表的 chat.read 和 chat.readwrite 权限。可以使用通过 Azure 注册的此 API 应用程序的用户可以访问它,并且用户具有 MFA 流。该用户不是内置 BOT,而是将与员工交互的实际 MS Teams 用户名和密码类型。
现在,我遇到了一些很大的困难(可能只是因为我对整个 Azure API 访问的工作原理的理解)如何获得正确的访问令牌以便能够读取用户群聊并与之交互。以下是我到目前为止的代码,我得到的回复是:
{
'error': 'invalid_grant',
'error_description': "AADSTS50076: Due to a configuration change made by your administrator, or because you moved to a new location, you must use multi-factor authentication to access
我尝试了一个解决方案,打开一个网络浏览器,我在其中输入用户并传递机器人,然后我这样做并输入短信代码密钥,它只是回复说我提供了错误的重定向 URI ..我签入了Azure 门户,我没有重定向 URI,但是我有一个应用程序 ID URI。
任何想法或指导将不胜感激。如果我忘记提供更多信息,请告诉我。
import requests
import msal
client_id = ''
client_secret = ''
tenant_id = ''
authority = f"https://login.microsoftonline.com/{tenant_id}"
scopes = ['https://graph.microsoft.com/.default']
app = msal.ConfidentialClientApplication(client_id, client_credential=client_secret, authority=authority)
username = ''
password = ''
result = app.acquire_token_by_username_password(username, password, scopes=scopes)
access_token = result['access_token']
代替
{'error': 'invalid_grant', 'error_description': "AADSTS50076: Due to a configuration change made by your administrator, or because you moved to a new location, you must use multi-factor authentication to access
我需要为用户获取访问令牌,以便我可以阅读其 MS 团队聊天。
我尝试在我的环境中重现相同的内容并得到以下结果:
我注册了一个 Azure AD 应用程序并授予
API permissions
如下所示:
我有一个 MFA-enabled 用户名为
Sri
如下所示:
当我运行下面的代码来获取访问令牌时,我得到了同样的错误,就像你喜欢的那样:
import msal
client_id = '97f9f251-459c-459f-8d53-xxxxxxxxxxxx'
client_secret = 'xxxxxxxxxxxxxxxxxxxx'
tenant_id = '3f5c7a77-062d-426c-8582-xxxxxxxxxxx'
authority = f"https://login.microsoftonline.com/{tenant_id}"
scopes = ['https://graph.microsoft.com/.default']
app = msal.ConfidentialClientApplication(client_id, client_credential=client_secret, authority=authority)
username = '[email protected]'
password = 'xxxxxxxxxxxx'
result = app.acquire_token_by_username_password(username, password, scopes=scopes)
print(result)
#access_token = result['access_token']
回复:
在这些场景中,您需要使用交互式流程需要用户登录的地方,通过修改您的代码如下:
import msal
client_id = '97f9f251-459c-459f-xxxxxxxxxxxxx'
tenant_id = '3f5c7a77-062d-4xxxxxxxxxxxxxxx'
authority = f"https://login.microsoftonline.com/{tenant_id}"
scopes = ['https://graph.microsoft.com/.default']
app = msal.PublicClientApplication(client_id, authority=authority)
result = app.acquire_token_interactive(scopes)
access_token = result['access_token']
print(access_token)
在您的情况下,请确保在您的应用程序中将 redirect URI 添加为 http://localhost,如下所示:
现在,我运行上面的代码,在浏览器中打开新选项卡,选择一个帐户 像这样登录:
当我从列表中选择名为 Sri
的MFA-enabled 用户时,它会要求验证身份,如下所示:
输入 OTP 后,我在屏幕下方看到 Authentication completed 消息如下:
当我检查输出时,我成功地获得了access token,如下所示:
为了 confirm,我在 jwt.ms 网站上解码了上面的令牌,该网站具有
scp
声明如下: