Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
GlynMThomas
Resolver I
Resolver 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
lbendlin
Super User
Super User

should be myorg, not MyTenantID

 

Does it work in the sandbox?

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

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
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.