<?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 PowerBI Admin account REST API Group calls fail with 401, GroupNotAccessible in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/PowerBI-Admin-account-REST-API-Group-calls-fail-with-401/m-p/1120941#M23880</link>
    <description>&lt;P&gt;I'm currently working on a powershell script to gather Power BI audit information for our BI team and export it to CSV so it can be used within Power BI.&amp;nbsp;My SSO account has been assigned the role of Power BI Admin so I can make the relevant REST API calls. Most calls I'm trying to make return data as expected, but any time I try to make a group API call (e.g. api.powerbi.com/v1.0/myorg/groups/*), I get a 401 error. I also get the same error when using the "try it" button on the documentation pages, logging in with my SSO account, then providing the relevant parameters. From the "REST API Try It" page, the headers show "&lt;SPAN&gt;x-powerbi-error-info:&amp;nbsp;GroupNotAccessible" when I get the 401s.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my powershell script, I first call the "Login-PowerBI" cmdlet to set context with my user. The login window appears and I select my sso user account. After that I start making calls. T&lt;SPAN&gt;his is an example call that works without error (please excuse my powershell code - I'm no expert):&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;# get gateways for use in subsequent calls
Write-Host "Retrieving gateways..."
$gatewayFileName = "C:\temp\gateways.csv"
[System.Collections.ArrayList]$gateways = @()
$gatewayInfo = Invoke-PowerBIRestMethod -Url "https://api.powerbi.com/v1.0/myorg/gateways" -Method Get | ConvertFrom-Json
if ($gatewayInfo) {
    if ($gatewayInfo[0].value) {
        foreach ($gw in $gatewayInfo[0].value) {
            $gateways.Add($gw)
        }        
    }
}
$gateways | ForEach-Object {$_}| Export-Csv -Path $gatewayFileName -NoTypeInformation&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I&amp;nbsp;get a list of gateways back and I create the file accordingly.&amp;nbsp; Here's an example call to get the refresh history for a given dataset within a group. This gives me a very generic "One or more errors occurred" error in powershell, but if I use "Resolve-PowerBIError -Last", I can see that the error is actually a 401 (unauthorized):&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;Write-Host "Retrieving dataset refresh history..."
$dsRefreshHistoryFileName = "C:\temp\dataset_refresh_history.csv"
[System.Collections.ArrayList]$dsRefreshHistory = @()
foreach ($groupId in $groupDatasets.Keys)
{
    foreach($gds in $groupDatasets["$groupId"])
    {
        $url = "https://api.powerbi.com/v1.0/myorg/groups/$($groupId)/datasets/$($gds.id)/refreshes"
        $url
        $histContainer = Invoke-PowerBIRestMethod -Url $url -Method Get | ConvertFrom-Json
        if ($histContainer) {
            if($histContainer[0].value)
            {
                foreach ($hist in $histContainer[0].value) {
                    $hist | Add-Member -NotePropertyName DatasetId -NotePropertyValue $gds.id
                    $dsRefreshHistory.Add($hist)
                }
            }
        }
    }
}
$dsRefreshHistory | ForEach-Object {$_} | Export-Csv -Path $dsRefreshHistoryFileName -NoTypeInformation&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The $groupDatasets variable is a hashtable populated by first retrieving the groups with this API:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;https://api.powerbi.com/v1.0/myorg/admin/groups?`$expand=dashboards&amp;amp;`$top=$topCount&amp;amp;`$skip=$skipCount&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then looping through each group ID and making this API call:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;https://api.powerbi.com/v1.0/myorg/admin/groups/$($group.id)/datasets&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This all seems to work as I get group data back from both of those calls.&amp;nbsp; However I don't appear to be able to use the non-Admin group API calls without the aforementioned error.&amp;nbsp;I don't think my powershell is at fault as I get the same error when using the same URL in the "REST API Try It" links from the documentation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is my account, with the PowerBI-Admin role, not allowed to make ALL REST API calls, or do I need to have rights to specific groups in order to pull that information?&amp;nbsp; The documentation calls out the required scope (e.g. "Workspace.Read.All" or "Workspace.ReadWrite.All"), but does having the Power BI admin role not give me that access?&amp;nbsp; My apologies if I've missed something fundamental here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your time!&lt;/P&gt;</description>
    <pubDate>Tue, 26 May 2020 20:49:02 GMT</pubDate>
    <dc:creator>bdineen</dc:creator>
    <dc:date>2020-05-26T20:49:02Z</dc:date>
    <item>
      <title>PowerBI Admin account REST API Group calls fail with 401, GroupNotAccessible</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/PowerBI-Admin-account-REST-API-Group-calls-fail-with-401/m-p/1120941#M23880</link>
      <description>&lt;P&gt;I'm currently working on a powershell script to gather Power BI audit information for our BI team and export it to CSV so it can be used within Power BI.&amp;nbsp;My SSO account has been assigned the role of Power BI Admin so I can make the relevant REST API calls. Most calls I'm trying to make return data as expected, but any time I try to make a group API call (e.g. api.powerbi.com/v1.0/myorg/groups/*), I get a 401 error. I also get the same error when using the "try it" button on the documentation pages, logging in with my SSO account, then providing the relevant parameters. From the "REST API Try It" page, the headers show "&lt;SPAN&gt;x-powerbi-error-info:&amp;nbsp;GroupNotAccessible" when I get the 401s.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my powershell script, I first call the "Login-PowerBI" cmdlet to set context with my user. The login window appears and I select my sso user account. After that I start making calls. T&lt;SPAN&gt;his is an example call that works without error (please excuse my powershell code - I'm no expert):&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;# get gateways for use in subsequent calls
Write-Host "Retrieving gateways..."
$gatewayFileName = "C:\temp\gateways.csv"
[System.Collections.ArrayList]$gateways = @()
$gatewayInfo = Invoke-PowerBIRestMethod -Url "https://api.powerbi.com/v1.0/myorg/gateways" -Method Get | ConvertFrom-Json
if ($gatewayInfo) {
    if ($gatewayInfo[0].value) {
        foreach ($gw in $gatewayInfo[0].value) {
            $gateways.Add($gw)
        }        
    }
}
$gateways | ForEach-Object {$_}| Export-Csv -Path $gatewayFileName -NoTypeInformation&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I&amp;nbsp;get a list of gateways back and I create the file accordingly.&amp;nbsp; Here's an example call to get the refresh history for a given dataset within a group. This gives me a very generic "One or more errors occurred" error in powershell, but if I use "Resolve-PowerBIError -Last", I can see that the error is actually a 401 (unauthorized):&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;Write-Host "Retrieving dataset refresh history..."
$dsRefreshHistoryFileName = "C:\temp\dataset_refresh_history.csv"
[System.Collections.ArrayList]$dsRefreshHistory = @()
foreach ($groupId in $groupDatasets.Keys)
{
    foreach($gds in $groupDatasets["$groupId"])
    {
        $url = "https://api.powerbi.com/v1.0/myorg/groups/$($groupId)/datasets/$($gds.id)/refreshes"
        $url
        $histContainer = Invoke-PowerBIRestMethod -Url $url -Method Get | ConvertFrom-Json
        if ($histContainer) {
            if($histContainer[0].value)
            {
                foreach ($hist in $histContainer[0].value) {
                    $hist | Add-Member -NotePropertyName DatasetId -NotePropertyValue $gds.id
                    $dsRefreshHistory.Add($hist)
                }
            }
        }
    }
}
$dsRefreshHistory | ForEach-Object {$_} | Export-Csv -Path $dsRefreshHistoryFileName -NoTypeInformation&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The $groupDatasets variable is a hashtable populated by first retrieving the groups with this API:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;https://api.powerbi.com/v1.0/myorg/admin/groups?`$expand=dashboards&amp;amp;`$top=$topCount&amp;amp;`$skip=$skipCount&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then looping through each group ID and making this API call:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;https://api.powerbi.com/v1.0/myorg/admin/groups/$($group.id)/datasets&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This all seems to work as I get group data back from both of those calls.&amp;nbsp; However I don't appear to be able to use the non-Admin group API calls without the aforementioned error.&amp;nbsp;I don't think my powershell is at fault as I get the same error when using the same URL in the "REST API Try It" links from the documentation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is my account, with the PowerBI-Admin role, not allowed to make ALL REST API calls, or do I need to have rights to specific groups in order to pull that information?&amp;nbsp; The documentation calls out the required scope (e.g. "Workspace.Read.All" or "Workspace.ReadWrite.All"), but does having the Power BI admin role not give me that access?&amp;nbsp; My apologies if I've missed something fundamental here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your time!&lt;/P&gt;</description>
      <pubDate>Tue, 26 May 2020 20:49:02 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/PowerBI-Admin-account-REST-API-Group-calls-fail-with-401/m-p/1120941#M23880</guid>
      <dc:creator>bdineen</dc:creator>
      <dc:date>2020-05-26T20:49:02Z</dc:date>
    </item>
    <item>
      <title>Re: PowerBI Admin account REST API Group calls fail with 401, GroupNotAccessible</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/PowerBI-Admin-account-REST-API-Group-calls-fail-with-401/m-p/1138737#M23988</link>
      <description>&lt;P&gt;I spoke with an Architect at Microsoft and they confirmed that my Power BI Admin user account still has to be a member of the group/workspace and dataset that I'm calling those particular APIs for. They were kind enough to point me to this article which details how permissions work in Power BI:&lt;/P&gt;&lt;P&gt;&lt;A href="https://www.sqlchick.com/entries/2018/12/1/how-permissions-work-for-a-power-bi-service-administrator" target="_blank"&gt;https://www.sqlchick.com/entries/2018/12/1/how-permissions-work-for-a-power-bi-service-administrator&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;After getting my account added to one of the sample groups and datasets, I was able to call the APIs in my script without getting the 401 error.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jun 2020 11:16:57 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/PowerBI-Admin-account-REST-API-Group-calls-fail-with-401/m-p/1138737#M23988</guid>
      <dc:creator>bdineen</dc:creator>
      <dc:date>2020-06-03T11:16:57Z</dc:date>
    </item>
  </channel>
</rss>

