【问题标题】:Invoke-RestMethod throwing forbidden error 403 while trying to access Power BIInvoke-RestMethod 在尝试访问 Power BI 时抛出禁止错误 403
【发布时间】:2021-09-22 02:52:26
【问题描述】:

我正在使用如下服务原理连接到 Power Bi。

$PbiSecurePassword = ConvertTo-SecureString $ClientSecret -Force -AsPlainText
$PbiCredential = New-Object Management.Automation.PSCredential($ClientId, $PbiSecurePassword)
Connect-PowerBIServiceAccount -ServicePrincipal -TenantId $TenantId -Credential $PbiCredential
$headers = Get-PowerBIAccessToken

然后我尝试更新数据集值,如下所示

$datasourePostUrl = "https://api.powerbi.com/v1.0/myorg/groups/$workspaceId/datasets/$datasetId/Default.UpdateDatasources"
# create HTTP request body to update datasource connection details
$postBody = @{
  "updateDetails" = @(
   @{
    "connectionDetails" = @{
      "server" = "$dbserver"
      "database" = "$dbname"
    }
    "datasourceSelector" = @{
      "datasourceType" = "Sql"
      "connectionDetails" = @{
        "server" = "$sqlDatabaseServerCurrent"
        "database" = "$sqlDatabaseNameCurrent"
      }
      "gatewayId" = "$gatewayId"
      "datasourceId" = "$datasourceId"
    }
  })
}

# convert body contents to JSON
$postBodyJson = ConvertTo-Json -InputObject $postBody -Depth 6 -Compress

#Added UseDefaultCredentials & UserAgent based on the stack overflow answer https://stackoverflow.com/a/16735376/6862041 & https://stackoverflow.com/a/27882588/6862041
$userAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::Chrome

# execute POST operation to update datasource connection details
Invoke-RestMethod -Headers $headers -Method Post -Uri $datasourePostUrl -UserAgent $userAgent -UseDefaultCredentials -Body $postBodyJson -ContentType $ContentType 

我收到以下错误

Exception        : System.Net.WebException: The remote server returned an error: (403) Forbidden.
 at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(WebRequest request
at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()
ErrorCategory    : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException

不知道我错过了什么。任何帮助将不胜感激。

【问题讨论】:

  • 403 错误表明用户凭据正确,但您连接的用户没有更新数据源连接的访问​​权限。
  • 我在 Azure Ad 中创建了此服务原则,并授予了此 Power Bi 工作区的权限。当我尝试将其嵌入我的网络应用程序时,这是有效的。但是当我尝试构建具有相同服务原则的 CI/CD 时,它不起作用。

标签: powershell azure-devops powerbi


【解决方案1】:

如果服务主体在工作区/数据集拥有必要的权限。

可能有一个设置(允许服务主体使用 Power BI API。 ) 在可能导致错误的租户级别 - 如果设置处于禁用状态 - 您可能会遇到此行为。

注意:默认处于禁用状态。

您必须以 Power BI 服务管理员

的身份访问以下内容

Power BI 管理门户 --> 租户设置 --> 开发人员设置 --> 允许服务主体使用 Power BI API。

将其切换到启用状态。

您可以为整个组织或属于安全组的特定服务主体集启用此功能。

点击“应用”。

【讨论】:

  • @joseph - 上述步骤对您有帮助吗?
猜你喜欢
  • 1970-01-01
  • 2020-02-17
  • 2013-03-02
  • 2016-10-22
  • 1970-01-01
  • 2015-05-14
  • 1970-01-01
  • 1970-01-01
  • 2015-01-09
相关资源
最近更新 更多