Forum Discussion

Juliana4u's avatar
Juliana4u
Frequent Visitor
2 years ago

Error 403 / Power Bi API

Hi there!

 

I am encountering an issue with my code while trying to obtain the access token via the Power BI API. In Azure Directory, everything is configured, and the necessary permissions are in place. In the Power BI admin portal, all settings related to the API are already enabled. I will share my Python code below, where I am encountering a 403 error. I have omitted sensitive information such as Client Secrets, App ID, Tenant ID, Username, and Password for security reasons.

Code:

import requests

def obter_token(client_id, client_secret, username, password, scope😞
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    data = {
        'grant_type': 'password',
        'client_id': client_id,
        'client_secret': client_secret,
        'scope': scope,
        'username': username,
        'password': password
    }

    response = requests.post(url, headers=headers, data=data)
    if response.status_code == 200:
        token = response.json().get('access_token')
        return token
    else:
        print(f"Erro na autenticação: {response.status_code}, {response.text}")
        return None


client_id = 'client_id'
client_secret = 'client_secret'
username = 'username'
password = 'password'

token = obter_token(client_id, client_secret, username, password, scope)

def teste(token😞
    headers = {
        'Authorization': f'Bearer {token}',
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    }
    print()
    response = requests.get(url, headers=headers)
    return response

if token:
    print(f"Token de acesso obtido com sucesso: {token}")
    func = teste(token)
    print(func)
   
Could someone please assist me with this code?

Thank you!



1 Reply

  • v-weiyan1-msft's avatar
    v-weiyan1-msft
    Community Support

    Hi Juliana4u ,

     

    Perhaps you can take a look at below article:
    Troubleshoot Power BI REST APIs - Power BI | Microsoft Learn

    A 403 error can occur for any of the following reasons. A Fiddler capture might be required to investigate further.

    • The user has exceeded the amount of embed token that can be generated on a shared capacity. Purchase Azure capacities to generate embed tokens and assign the workspace to that capacity. See Create Power BI Embedded capacity in the Azure portal.
    • The Microsoft Entra authorization token expired.
    • The authenticated user isn't a member of the group (workspace).
    • The authenticated user isn't an admin of the group (workspace).
    • The authenticated user doesn't have permissions. Permissions can be updated using the refreshUserPermissions API.
    • The authorization header might not be listed correctly. Make sure there are no typos.

    In addition, the following link had the similar problem as yours. Please refer to it and see if it helps you.

    Solved: 403 error Power BI rest API - Microsoft Fabric Community

     

    Best Regards,
    Yulia Yan

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