在Python中获取无客户端id的Power BI服务访问令牌

在Python中获取无客户端id的Power BI服务访问令牌,python,python-3.x,powerbi,adal,msal,Python,Python 3.x,Powerbi,Adal,Msal,我可以使用PowerShell cmdlet MicrosoftPowerBIMgmt,使用(1)用户名和(2)密码参数输入获取访问令牌。它工作得很好。请注意,它不需要client_id参数: # PowerShell code: Import-Module MicrosoftPowerBIMgmt Function authenticate_powerbiserviceaccount($us, $pw) { # Convert plain text pw to secureStri

我可以使用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函数来获取给定用户名和密码的auth_字符串,而不需要客户端id?它看起来怎么样?

实际上,PowerShell命令
获取PowerBIAccessToken
也使用
客户端id
,它是一个名为
Power BI Gateway
的著名应用程序,其
客户端id
ea0616ba-638b-4df5-95b9-636659ae5121
。要检查这一点,只需使用fiddler捕获此命令的请求

是否可以构建一个python函数来获取给定用户名和密码的auth_字符串,而不需要客户端id

所以回答你的问题,这是不可能的。ADAL基本上使用,需要
客户端id

在您的情况下,如果您没有客户端应用程序,只需在代码中直接使用著名的客户端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)

实际上,PowerShell命令
获取PowerBIAccessToken
也使用
客户端id
,它是一个名为
Power BI Gateway
的著名应用程序,其
客户端id
ea0616ba-638b-4df5-95b9-636659ae5121
。要检查这一点,只需使用fiddler捕获此命令的请求

是否可以构建一个python函数来获取给定用户名和密码的auth_字符串,而不需要客户端id

所以回答你的问题,这是不可能的。ADAL基本上使用,需要
客户端id

在您的情况下,如果您没有客户端应用程序,只需在代码中直接使用著名的客户端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)