【问题标题】:Acquire Power BI Service Access token in Python without client_id在没有 client_id 的 Python 中获取 Power BI 服务访问令牌
【发布时间】:2021-01-26 10:55:59
【问题描述】:

我可以使用 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 的身份验证 sn-ps,但它们都需要一个我没有且无法获取的 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_string 而无需 client_id?它看起来怎么样?

【问题讨论】:

    标签: python python-3.x powerbi adal msal


    【解决方案1】:

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

    是否可以构建一个 python 函数来获取给定用户名和密码的 auth_string 而无需 client_id?

    所以回答你的问题,这是不可能的。 ADAL 本质上使用ROPC flowclient_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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      • 1970-01-01
      • 2018-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多