【问题标题】:Rename Power BI dataflow using Power BI REST API - 401 Unauthorised使用 Power BI REST API 重命名 Power BI 数据流 - 401 Unauthorized
【发布时间】:2021-06-07 08:25:58
【问题描述】:

我想使用 Python 使用 Power BI API 端点 update dataflow 重命名 Power BI 租户中的数据流,但我收到授权错误。

按照Microsoft guidance我有:

  • 创建 Azure 应用
  • 授予Dataflow.ReadWrite.All 权限
  • 将服务主体添加到安全组
  • 更新了 Power BI 租赁设置以允许新的安全组运行 Power BI API
  • 向安全组和服务主体授予对工作区的访问权限

当我运行下面的代码时,我收到了<Response [401]>,但是我为其他端点编写的类似代码,例如get dataflowrefresh dataflow 工作正常。如果我从 Azure 应用程序中删除所有 Power BI API 权限,它仍然能够运行 get 和 refresh 调用。就像代码正在使用工作区的用户权限而不是获取应用程序权限。

非常感谢任何帮助。

谢谢

Python

import adal
import requests

TENANT_ID = "abc-123"
CLIENT_ID = "def-456"
CLIENT_SECRET = "ghi-789"
WORKSPACE_ID = "jkl-987"
DATAFLOW_ID = "mno-654"

authority_url = "https://login.microsoftonline.com/"+TENANT_ID

context = adal.AuthenticationContext(authority_url)

token = context.acquire_token_with_client_credentials(
    resource="https://analysis.windows.net/powerbi/api",
    client_id=CLIENT_ID,
    client_secret=CLIENT_SECRET,
)

access_token = token.get("accessToken")
token_type = token.get("tokenType")

tokenString = "{} {}".format(token_type, access_token)

header = {"Authorization": tokenString}

newName = "This is a new name"
body = {"name": newName}

refresh_url = "https://api.powerbi.com/v1.0/myorg/groups/{}/dataflows/{}".format(WORKSPACE_ID, DATAFLOW_ID) #get all meta for specific dataflow

r = requests.patch(url=refresh_url, headers=header, json = body)

print(r)

【问题讨论】:

  • 似乎权限Dataflow.ReadWrite.All 是委托类型,但是您在代码中使用client credential 流来获取访问令牌。那么您能否使用username/password flow 在您的代码中获取访问令牌?
  • 请尝试使用methodmethod
  • 嗨西蒙,有什么更新吗?
  • 嗨@HuryShen 感谢您的帮助。我现在尝试使用用户名/密码方法,但得到AADSTS53003: Access has been blocked by Conditional Access policies.The access policy does not allow token issuance. 所以看起来为用户帐户颁发令牌的能力已被我们的租户阻止。我会尝试 acquire_token_with_authorization_code 但我不知道如何获取授权码。
  • 请问您的问题是否已解决?如果问题解决了,能否请您将答案标记为“已接受”,提前谢谢。

标签: python azure powerbi adal


【解决方案1】:

根据您在 cmets 中提供的描述,您似乎启用了 MFA。所以当你使用username/password流时,会被阻塞。

所以你可以使用auth code flow 来获取访问令牌。您需要使用方法acquire_token_with_authorization_code(authorization_code, redirect_uri, resource, client_id, client_secret=None, code_verifier=None)。我想你知道方法中的所有参数,除了authorization_code。您可以参考此document 了解如何获取它。

请求url,参数跟随url,它将重定向到您在请求中指定的重定向uri,并跟随名为code=...的参数。 code 的值为authorization_code

在这里我提供我的请求示例(使用验证码流)供您参考:

我请求网址:https://login.microsoftonline.com/xxxxx/oauth2/v2.0/authorize?client_id=xxxxx&response_type=code&redirect_uri=https://hurytest&response_mode=query&scope=https://graph.microsoft.com/.default&state=12345&nonce=67890

它会要求我登录。登录后,它会重定向到 uri https://hurytest/,后面跟着code

codeauthorization code。您可以使用代码进行下一步。

顺便提一下请求的scoperesource。我在示例中使用 v2.0 端点,所以我使用 scope 而不是 resource,但我注意到您在代码中使用 resource(v1.0 端点使用 resource)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-06
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    相关资源
    最近更新 更多