问题描述
我可以使用 PowerShell cmdlet MicrosoftPowerBIMgmt 通过 (1) 用户名和 (2) 密码参数输入获取访问令牌。它完美地工作。请注意,它不需要 client_id 参数:
# PowerShell code:
Import-Module MicrosoftPowerBIMgmt
Function authenticate_powerbiserviceaccount($us,$pw)
{
# Convert plain text pw to securestring
$pw_secure = $pw | ConvertTo-securestring -asPlainText -Force
# Authenticate
$credential = New-Object System.Management.Automation.PSCredential($us,$pw_secure)
Connect-PowerBIServiceAccount -Credential $credential
# Print access token auth_string
Get-PowerBIAccesstoken -Asstring
}
我发现了使用 ADAL 的身份验证片段,但它们都需要一个 client_id 参数,而我没有,也无法获取。
# Python code:
import adal
authority_url = 'https://login.windows.net/common'
context = adal.AuthenticationContext(
authority_url,validate_authority=True,api_version=None
)
token = context.acquire_token_with_username_password(
resource='https://analysis.windows.net/powerbi/api',username=user_string,password=pw_string,client_id=client_id_string
)
access_token = token['accesstoken']
是否可以构建一个 python 函数,在不需要 client_id 的情况下获取给定用户名和密码的 auth_string?看起来怎么样?
解决方法
实际上,PowerShell 命令Get-PowerBIAccessToken
也使用了一个client_id
,它是一个名为Power BI Gateway
的知名应用程序,其client_id
为ea0616ba-638b-4df5-95b9-636659ae5121
。要检查这一点,只需使用 fiddler 捕获此命令的请求即可。
是否可以构建一个 python 函数来获取给定用户名和密码的 auth_string 而不需要 client_id?
所以回答你的问题,这是不可能的。 ADAL 本质上使用 ROPC flow,client_id
是必需的。
就您而言,如果您没有客户端应用程序,只需直接在代码中使用众所周知的 client_id ea0616ba-638b-4df5-95b9-636659ae5121
,即可轻松获取令牌。
示例:
import adal
AUTHORITY_URL = 'https://login.windows.net/common'
RESOURCE = 'https://analysis.windows.net/powerbi/api'
CLIENT_ID = 'ea0616ba-638b-4df5-95b9-636659ae5121'
userid='xxx@xxx.onmicrosoft.com'
userpassword='xxxx'
context = adal.AuthenticationContext(AUTHORITY_URL,validate_authority=True,api_version=None)
token = context.acquire_token_with_username_password(RESOURCE,userid,userpassword,CLIENT_ID)
access_token = token['accessToken']
print(access_token)