<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic REST-API Dataset Refresh Using Azure Functions and Powershell Error 401 in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/REST-API-Dataset-Refresh-Using-Azure-Functions-and-Powershell/m-p/1214609#M24679</link>
    <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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&amp;nbsp;&lt;SPAN&gt;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):&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;using namespace System.Net&lt;BR /&gt;using namespace Microsoft.PowerShell.Utility&lt;/P&gt;&lt;P&gt;# Input bindings are passed in via param block.&lt;BR /&gt;param($Request, $TriggerMetadata)&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;#read groupName and datasetName from request&lt;BR /&gt;$groupId = "MyGroupId"&lt;BR /&gt;$datasetId = "MyDatasetId"&lt;/P&gt;&lt;P&gt;#Auth&lt;/P&gt;&lt;P&gt;$ClientId = "MyClientId"&lt;BR /&gt;$ClientSecret = "MySecret"&lt;BR /&gt;$Resource = "&lt;A href="https://analysis.windows.net/powerbi/api" target="_blank" rel="noopener"&gt;https://analysis.windows.net/powerbi/api&lt;/A&gt;"&lt;/P&gt;&lt;P&gt;$authUrl = "&lt;A href="https://login.windows.net/common/oauth2/token/”" target="_blank" rel="noopener"&gt;https://login.windows.net/common/oauth2/token/”&lt;/A&gt;&lt;/P&gt;&lt;P&gt;$grantType = "client_credentials"&lt;/P&gt;&lt;P&gt;$body = "grant_type=$grantType&amp;amp;client_id=$ClientId&amp;amp;client_secret=$ClientSecret&amp;amp;resource=$Resource"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;$authResponse = Invoke-RestMethod -Uri $authUrl –Method POST -Body $body&lt;/P&gt;&lt;P&gt;#Write-Output $authResponse&lt;/P&gt;&lt;P&gt;# Building Rest API header with authorization token&lt;BR /&gt;$authHeader = @{&lt;BR /&gt;"Content-Type" = "application/json";&lt;BR /&gt;"Authorization" = $authResponse.token_type + " " + $authResponse.access_token&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;#Write-Output $authResponse.access_token&lt;/P&gt;&lt;P&gt;$restURL = “&lt;A href="https://api.powerbi.com/v1.0/MytenantId/groups/$groupId/datasets/$datasetId/refreshes”" target="_blank" rel="noopener"&gt;https://api.powerbi.com/v1.0/MytenantId/groups/$groupId/datasets/$datasetId/refreshes”&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Invoke-RestMethod -Uri $restURL –Method POST -Headers $authHeader&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get the following error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;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 &amp;lt;ScriptBlock&amp;gt;, 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)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 10 Jul 2020 12:14:39 GMT</pubDate>
    <dc:creator>GlynMThomas</dc:creator>
    <dc:date>2020-07-10T12:14:39Z</dc:date>
    <item>
      <title>REST-API Dataset Refresh Using Azure Functions and Powershell Error 401</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/REST-API-Dataset-Refresh-Using-Azure-Functions-and-Powershell/m-p/1214609#M24679</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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&amp;nbsp;&lt;SPAN&gt;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):&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;using namespace System.Net&lt;BR /&gt;using namespace Microsoft.PowerShell.Utility&lt;/P&gt;&lt;P&gt;# Input bindings are passed in via param block.&lt;BR /&gt;param($Request, $TriggerMetadata)&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;#read groupName and datasetName from request&lt;BR /&gt;$groupId = "MyGroupId"&lt;BR /&gt;$datasetId = "MyDatasetId"&lt;/P&gt;&lt;P&gt;#Auth&lt;/P&gt;&lt;P&gt;$ClientId = "MyClientId"&lt;BR /&gt;$ClientSecret = "MySecret"&lt;BR /&gt;$Resource = "&lt;A href="https://analysis.windows.net/powerbi/api" target="_blank" rel="noopener"&gt;https://analysis.windows.net/powerbi/api&lt;/A&gt;"&lt;/P&gt;&lt;P&gt;$authUrl = "&lt;A href="https://login.windows.net/common/oauth2/token/”" target="_blank" rel="noopener"&gt;https://login.windows.net/common/oauth2/token/”&lt;/A&gt;&lt;/P&gt;&lt;P&gt;$grantType = "client_credentials"&lt;/P&gt;&lt;P&gt;$body = "grant_type=$grantType&amp;amp;client_id=$ClientId&amp;amp;client_secret=$ClientSecret&amp;amp;resource=$Resource"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;$authResponse = Invoke-RestMethod -Uri $authUrl –Method POST -Body $body&lt;/P&gt;&lt;P&gt;#Write-Output $authResponse&lt;/P&gt;&lt;P&gt;# Building Rest API header with authorization token&lt;BR /&gt;$authHeader = @{&lt;BR /&gt;"Content-Type" = "application/json";&lt;BR /&gt;"Authorization" = $authResponse.token_type + " " + $authResponse.access_token&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;#Write-Output $authResponse.access_token&lt;/P&gt;&lt;P&gt;$restURL = “&lt;A href="https://api.powerbi.com/v1.0/MytenantId/groups/$groupId/datasets/$datasetId/refreshes”" target="_blank" rel="noopener"&gt;https://api.powerbi.com/v1.0/MytenantId/groups/$groupId/datasets/$datasetId/refreshes”&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Invoke-RestMethod -Uri $restURL –Method POST -Headers $authHeader&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get the following error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;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 &amp;lt;ScriptBlock&amp;gt;, 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)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jul 2020 12:14:39 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/REST-API-Dataset-Refresh-Using-Azure-Functions-and-Powershell/m-p/1214609#M24679</guid>
      <dc:creator>GlynMThomas</dc:creator>
      <dc:date>2020-07-10T12:14:39Z</dc:date>
    </item>
    <item>
      <title>Re: REST-API Dataset Refresh Using Azure Functions and Powershell Error 401</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/REST-API-Dataset-Refresh-Using-Azure-Functions-and-Powershell/m-p/1215743#M24690</link>
      <description>&lt;P&gt;should be myorg, not MyTenantID&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does it work in the sandbox?&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.microsoft.com/en-us/rest/api/power-bi/datasets/refreshdatasetingroup" target="_blank" rel="noopener"&gt;https://docs.microsoft.com/en-us/rest/api/power-bi/datasets/refreshdatasetingroup&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jul 2020 23:35:52 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/REST-API-Dataset-Refresh-Using-Azure-Functions-and-Powershell/m-p/1215743#M24690</guid>
      <dc:creator>lbendlin</dc:creator>
      <dc:date>2020-07-10T23:35:52Z</dc:date>
    </item>
    <item>
      <title>Re: REST-API Dataset Refresh Using Azure Functions and Powershell Error 401</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/REST-API-Dataset-Refresh-Using-Azure-Functions-and-Powershell/m-p/1217966#M24704</link>
      <description>&lt;P&gt;It works with either myorg or the tenant id.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jul 2020 09:29:17 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/REST-API-Dataset-Refresh-Using-Azure-Functions-and-Powershell/m-p/1217966#M24704</guid>
      <dc:creator>GlynMThomas</dc:creator>
      <dc:date>2020-07-13T09:29:17Z</dc:date>
    </item>
  </channel>
</rss>

