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
gddata
New Member

Power BI Rest API returns 401 authorized for some methods

I want to prepare a report that shows the usage statistics of our PowerBI reports. I wanted to use the PowerBI Rest API for this. I was able to access this API and do a few operations, but we cannot access other methods (Admin) that we want to access (such as creating a group, listing the group we created). We are getting a 401 error. I think that we have given the necessary authorizations on Azure and through Power BI Admin, but I think there is a point we missed.

 

PowerBI Portal Admin SettingsPowerBI Portal Admin Settings

 

 

 

 

RESOURCE_URL = 'https://analysis.windows.net/powerbi/api/.default' 
GROUP_URL = "https://api.powerbi.com/v1.0/myorg/groups"
ADMIN_URL = "https://api.powerbi.com/v1.0/myorg/admin/apps"
ADMIN_GROUP_URL_BASE = "https://api.powerbi.com/v1.0/myorg/admin/groups"
REPORT_URL = "https://api.powerbi.com/v1.0/myorg/groups/{}/reports"

token = { "bearer": None, "expiration": None }

headers = { "Content-Type': 'application/json" }
#response = requests.post("https://login.microsoftonline.com/", headers = headers, data = body)

auth = ClientSecretCredential (authority = AUTHORITY_URL, tenant_id = TENANT_ID, client_id = CLIENT_ID, client_secret = CLIENT_SECRET)
access_token = auth.get_token(RESOURCE_URL)
access_token = access_token.token
print(access_token)

bearer = access_token
token["bearer"] = "Bearer {}".format(bearer)
token["expiration"] = datetime.datetime.now() + datetime.timedelta(hours = 1)
rint(token)
headers = { "Authorization": token["bearer"] }

def CreateGroup(group_name:str):
    workspace_v2=True
    body = {
      "name": group_name
    }

    parameters = "?workspaceV2=True"
    
    response = requests.post(GROUP_URL + parameters, headers = headers, data = body)
    
    if response.status_code == HTTP_OK:
        print("New group created")

def GetGroups():
    response = requests.get(GROUP_URL, headers = headers)
    print(response)
    if response.status_code == HTTP_OK:
        print(response.json()["value"])

GetGroups()

 

 

 

 

Response :

https://api.powerbi.com/v1.0/myorg/admin/groups?$top={$top}
<Response [401]>

Please could you  suggest a way to solve this problem.

 

Thanks.

1 ACCEPTED SOLUTION
v-yiruan-msft
Community Support
Community Support

Hi @gddata ,

According to your provided info, it seems that you call REST API using service principal. But service principal authentication is currently supported for the following read-only admin APIs. You can find the details in the following official documentations.

Considerations and limitations

Supported APIs

vyiruanmsft_0-1692093156178.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-yiruan-msft
Community Support
Community Support

Hi @gddata ,

According to your provided info, it seems that you call REST API using service principal. But service principal authentication is currently supported for the following read-only admin APIs. You can find the details in the following official documentations.

Considerations and limitations

Supported APIs

vyiruanmsft_0-1692093156178.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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.