Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
charly805
New Member

403 Error When Trying to Embed Report With PHP and Curl

Hello,

 

This is the code that I have:

 

 

 

 

 

 

<?php
    $clientId = 'xxx';
    $clientSecret = 'xxx';
    $tenantId = 'xxx';
    $urlAccessToken = "https://login.windows.net/$tenantId/oauth2/token";
    $resource = 'https://analysis.windows.net/powerbi/api';
    $group = 'xxx';
    $report ='xxx';

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "https://login.microsoftonline.com/$tenantId/oauth2/token");

    curl_setopt($ch, CURLOPT_POST, TRUE);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    'resource' => $resource,
    'client_id' => $clientId,
    'client_secret' => $clientSecret,
    'grant_type' => 'client_credentials'
    ));

    $data = curl_exec($ch);
    curl_close($ch);

    $data_obj = json_decode($data);
    $access_token = $data_obj->{"access_token"};
    $headers = array(
    "Content-Type: application/json",
    "Authorization: Bearer $access_token"
    );


    $url = "https://api.powerbi.com/v1.0/myorg/groups/$group/reports/$report/GenerateToken";
    #$url ="https://api.powerbi.com/v1.0/myorg/groups/$group/reports";
    $post_params = array(
    'accessLevel' => 'View'
    );

    $payload = json_encode($post_params);

    $ch2 = curl_init( $url );
    curl_setopt( $ch2, CURLINFO_HEADER_OUT, true);
    curl_setopt( $ch2, CURLOPT_POSTFIELDS, $payload);
    curl_setopt( $ch2, CURLOPT_HTTPHEADER, $headers );
    curl_setopt( $ch2, CURLOPT_RETURNTRANSFER, true);
    curl_setopt( $ch2, CURLOPT_FAILONERROR, true);
    $response = curl_exec( $ch2 );


    if (curl_errno($ch2)) {
        $error_msg = curl_error($ch2);
    }
    echo $response;
    curl_close($ch2);

    if (isset($error_msg)) {
        echo $error_msg;
    }

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="/powerbi.js"></script>
</head>
<body>
    <div id="reportContainer"></div>
</body>
<script>
    // Get models. models contains enums that can be used.
    var models = window['powerbi-client'].models;
    // Embed configuration used to describe the what and how to embed.
    // This object is used when calling powerbi.embed.
    // This also includes settings and options such as filters.
    // You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.
    var embedConfiguration= {
        type: 'report',
        id: 'xxx', // the report ID
        embedUrl: "<?php echo $url ?>",
        accessToken: "<?php echo $access_token; ?>" ,
    };
    var $reportContainer = $('#reportContainer');
    var report = powerbi.embed($reportContainer.get(0), embedConfiguration);
</script>
</html>

 

 

 

 

Thanks in advance!

1 ACCEPTED SOLUTION
V-lianl-msft
Community Support
Community Support

Please make sure you are sending correct Token in the authorization header. 

Also, make sure your user has all required permissions.

https://docs.microsoft.com/en-us/power-bi/developer/embedded/generate-embed-token 

You can also refer to this blog for the same sample:

https://www.msbiblog.com/2018/01/12/power-bi-embedded-example-using-curl-and-php/ 

View solution in original post

1 REPLY 1
V-lianl-msft
Community Support
Community Support

Please make sure you are sending correct Token in the authorization header. 

Also, make sure your user has all required permissions.

https://docs.microsoft.com/en-us/power-bi/developer/embedded/generate-embed-token 

You can also refer to this blog for the same sample:

https://www.msbiblog.com/2018/01/12/power-bi-embedded-example-using-curl-and-php/ 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.