简体   繁体   English

在没有 client_id 的 Python 中获取 Power BI 服务访问令牌

[英]Acquire Power BI Service Access token in Python without client_id

I can use the the PowerShell cmdlet MicrosoftPowerBIMgmt to get an access token using (1) username and (2) password parameters inputs.我可以使用 PowerShell cmdlet MicrosoftPowerBIMgmt 使用 (1) 用户名和 (2) 密码参数输入来获取访问令牌。 It works perfectly.它完美地工作。 Note that it doesn't require an client_id parameter:请注意,它不需要 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
}

I've found authentication snippets that use ADAL but they all require a client_id parameter which I don't have and I am not able to get.我找到了使用 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']

Is it possible to build a python function that gets the auth_string for a given username and password without the need for a client_id?是否可以构建一个 python function 来获取给定用户名和密码的 auth_string 而无需 client_id? How does it look like?它看起来怎样?

Actually, the PowerShell command Get-PowerBIAccessToken also uses a client_id , it is a well-known application named Power BI Gateway , whose client_id is ea0616ba-638b-4df5-95b9-636659ae5121 .实际上,PowerShell 命令Get-PowerBIAccessToken也使用了client_id ,这是一个名为Power BI Gateway的知名应用程序,其client_idea0616ba-638b-4df5-95b9-636659ae5121 To check this, just use fiddler to catch the request of this command.要检查这一点,只需使用 fiddler 捕获此命令的请求。

在此处输入图像描述

Is it possible to build a python function that gets the auth_string for a given username and password without the need for a client_id?是否可以构建一个 python function 来获取给定用户名和密码的 auth_string 而无需 client_id?

So answer your question, it is not possible.所以回答你的问题,这是不可能的。 The ADAL essentially uses the ROPC flow , the client_id is required. ADAL 本质上使用ROPC 流,需要client_id

In your case, if you don't have a client app, you just need to use the well-known client_id ea0616ba-638b-4df5-95b9-636659ae5121 directly in your code, then you can get the token easily.在您的情况下,如果您没有客户端应用程序,则只需在代码中直接使用众所周知的 client_id ea0616ba-638b-4df5-95b9-636659ae5121 ,即可轻松获取令牌。

Sample:样本:

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)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM