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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Kudoed Authors