我使用tutorial通过AAD对Power BI REST API进行身份验证。
本教程使用C#和ADAL。它通过,
AuthenticationContext authContext = new AuthenticationContext(authorityUri);
string token = authContext.AcquireToken(resourceUri, clientID, new Uri(redirectUri)).AccessToken;
我更喜欢使用Python,因此我尝试使用ADAL library for Python。
我可以通过,
context = adal.AuthenticationContext(authority_url)
但我找不到与AcquireToken等效的Python ADAL函数。最近的选项似乎是
context.acquire_token
,但这需要我提供C#版本不提供的user_id
参数(它也不接受重定向URI参数)。如何在Python中获得与教程的C#代码中相同的行为?
最佳答案
(我的意思是将此作为对Marcus's answer中对话的评论,但它太长了,无法容纳)
感谢马库斯指出一个工作样本和博客文章。然而,值得指出的是:
作为应用程序进行身份验证和作为最终用户进行身份验证之间存在巨大的概念差异。它们是不可交换的。似乎@JamesElderfield想要以交互方式验证最终用户,在这种情况下acquire_token_with_client_credentials(...)
不是您要查找的机器人。
这个C#authContext.AcquireToken(resourceUri, clientID, new Uri(redirectUri))
等价物大致上是ADAL Python中的acquire_token_with_authorization_code(...)
,但事实上目前还没有,尽管在ADAL Python for native app(也称为public client)中是already supported,但它需要应用程序开发人员实现自己的逻辑来启动HTTP服务器来捕获重定向的auth代码。
如果上面的选项听起来对您来说太复杂,那么您可以选择使用Device Code flow,这是@abhidnya 's answer正确建议的。
关于c# - 与C#ADAL AuthenticationContext.AcquireToken等效的Python,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49539048/