Forum Discussion

ViralGajjar2904's avatar
ViralGajjar2904
Frequent Visitor
2 years ago
Solved

Refresh API getting wrong data

I published below report in powerbi server by updating latest SessionId & BaseURL based on Client's interaction from webpage.   let     body="{""sessionId"": """& SessionIDPara &""",     ""isSear...
  • hackcrr's avatar
    hackcrr
    2 years ago

    Hi, ViralGajjar2904 

    Here is a sample API call to generate an embed token:

    import requests
    import json
    
    def generate_embed_token(username, report_id, dataset_id, workspace_id):
        url = "https://api.powerbi.com/v1.0/myorg/reports/{report_id}/GenerateToken"
    
        payload = json.dumps({
            "accessLevel": "View",
            "identities": [
                {
                    "username": username,
                    "roles": ["Viewer"],
                    "datasets": [dataset_id]
                }
            ]
        })
        headers = {
            'Authorization': 'Bearer YOUR_ACCESS_TOKEN',
            'Content-Type': 'application/json'
        }
    
        response = requests.request("POST", url.format(report_id=report_id), headers=headers, data=payload)
    
        if response.status_code == 200:
            return response.json()['token']
        else:
            raise Exception(f"Error generating embed token: {response.text}")
    
    # Example usage:
    embed_token = generate_embed_token("[email protected]", "report_id", "dataset_id", "workspace_id")
    

     

    Ensure that your embedded report uses the token generated for the specific user session. This can be done in your client-side code where the Power BI report is embedded:

    var embedToken = 'USER_SPECIFIC_EMBED_TOKEN';
    var reportId = 'YOUR_REPORT_ID';
    var groupId = 'YOUR_GROUP_ID';
    var embedUrl = `https://app.powerbi.com/reportEmbed?reportId=${reportId}&groupId=${groupId}`;
    
    var models = window['powerbi-client'].models;
    var embedConfig = {
        type: 'report',
        tokenType: models.TokenType.Embed,
        accessToken: embedToken,
        embedUrl: embedUrl,
        id: reportId,
        permissions: models.Permissions.All,
        settings: {
            filterPaneEnabled: false,
            navContentPaneEnabled: true
        }
    };
    
    // Embed the report and display it within the div container.
    var reportContainer = document.getElementById('reportContainer');
    var report = powerbi.embed(reportContainer, embedConfig);
    

     

    Use different browser sessions or incognito windows to simulate multiple users accessing the report. Ensure that each session generates and uses a unique embed token.

     

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