cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Helper I
Helper I

REST-API Dataset Refresh Using Azure Functions and Powershell Error 401

Hi All,

 

I've been trying to setup an azure function to refresh our datasets so I can call it from the data factory and also check the refresh status so we can manage them more efficiently and also levi more resource during the run. I'm getting a 401 (Unauthorized) error however when trying to call the power bi rest api however and I've been wracking my brain trying to figure out why. I'm using the following code in my function to refresh (Obviously with the sensitive info removed):

 

using namespace System.Net
using namespace Microsoft.PowerShell.Utility

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)


#read groupName and datasetName from request
$groupId = "MyGroupId"
$datasetId = "MyDatasetId"

#Auth

$ClientId = "MyClientId"
$ClientSecret = "MySecret"
$Resource = "https://analysis.windows.net/powerbi/api"

$authUrl = "https://login.windows.net/common/oauth2/token/”

$grantType = "client_credentials"

$body = "grant_type=$grantType&client_id=$ClientId&client_secret=$ClientSecret&resource=$Resource"

 

$authResponse = Invoke-RestMethod -Uri $authUrl –Method POST -Body $body

#Write-Output $authResponse

# Building Rest API header with authorization token
$authHeader = @{
"Content-Type" = "application/json";
"Authorization" = $authResponse.token_type + " " + $authResponse.access_token
}

#Write-Output $authResponse.access_token

$restURL = “https://api.powerbi.com/v1.0/MytenantId/groups/$groupId/datasets/$datasetId/refreshes”

Invoke-RestMethod -Uri $restURL –Method POST -Headers $authHeader

 

I get the following error:

 

2020-07-10T11:55:07Z [Error] ERROR: Invoke-RestMethod : Response status code does not indicate success: 401 (Unauthorized). At D:\home\site\wwwroot\Refresh-PowerBi\run.ps1:40 char:1 + Invoke-RestMethod -Uri $restURL –Method POST -Headers $authHeader + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (Method: POST, Reque\u2026PowerShell/6.2.4 }:HttpRequestMessage) [Invoke-RestMethod], HttpResponseException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand Script stack trace: at <ScriptBlock>, D:\home\site\wwwroot\Refresh-PowerBi\run.ps1: line 40 Microsoft.PowerShell.Commands.HttpResponseException: Response status code does not indicate success: 401 (Unauthorized). at System.Management.Automation.MshCommandRuntime.ThrowTerminatingError(ErrorRecord errorRecord)

 

I have a registered application setup with a secret setup on it. By the looks of it I can pull the oauth token successfully as I've manage to write that to the logs when removing the power bi rest invoke. I just can't seem to get it to authenticate and kick off the refresh.

 

 

2 REPLIES 2
Highlighted
Super User V
Super User V

Re: REST-API Dataset Refresh Using Azure Functions and Powershell Error 401

should be myorg, not MyTenantID

 

Does it work in the sandbox?

https://docs.microsoft.com/en-us/rest/api/power-bi/datasets/refreshdatasetingroup

Highlighted
Helper I
Helper I

Re: REST-API Dataset Refresh Using Azure Functions and Powershell Error 401

It works with either myorg or the tenant id.

 

Yes it works in sandbox and I can take the token from sandbox and hardcode it into my function and it will work until the token expires. The issue seems to be with the token being generated from the registered application.

Helpful resources

Announcements
Meet the 2020 Season 2 Power BI Super Users!

Meet the 2020 Season 2 Power BI Super Users!

Find out who's part of the program this season, and welcome the new Super Users.

August 2020 Community Challenge: Can You Solve These?

August 2020 Community Challenge: Can You Solve These?

We're excited to announce our first cross-community 'Can You Solve These?' challenge!

July 2020 Community Highlights

July 2020 Community Highlights

Learn about the exciting things that happened in July.

Featured Data Story of The Month

Featured Data Story of The Month

All Data Stories Gallery contributions are reviewed for each month. We select a contribution and feature the community member the following month.

Power BI Dev Camp - Developing with .NET Core

Power BI Dev Camp - Developing with .NET Core

Learn how to develop custom web applications for Power BI using .NET Core 3.1 and .NET 5.