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

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
MarkLuprin
New Member

Not giving token 403 error

I've looked through all the forums I've found, but I haven't seen a solution.

The problem is that I am using the given code:

 

        $curlPostToken = curl_init();
        $clientId = "******";
        $clientSecretId = "******";
        $tenantId = "******";
        $workspace = "******";
        $ReportId = "******";
        
        curl_setopt_array($curlPostToken, array(
            CURLOPT_URL => "https://login.microsoftonline.com/$tenantId/oauth2/token",            
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => array(
                'grant_type' => 'client_credentials',
                'scope' => '.default',
                'resource' => 'https://analysis.windows.net/powerbi/api',
                'tenant_id' => $tenantId,                
                'client_id' => $clientId, // Registered App Application ID                
                'client_secret' => $clientSecretId,                
            )
        ));

        $tokenResponse = curl_exec($curlPostToken);
        $tokenError = curl_error($curlPostToken);
		
        echo $tokenError;
        // decode result, and store the access_token in $embeddedToken variable:
        $tokenResult = json_decode($tokenResponse, true);
        $token = $tokenResult["access_token"];
        $bearerToken = "Bearer "  . ' ' .  $token;
       
        $post_params = array(
            "reports" => Array(                
                Array('id'=>$ReportId),

            ),
            "accessLevel"=>"View",
        );

        $payload = json_encode($post_params);        
        
		$headers = array(
		"Content-Type: application/json",
		"Authorization: Bearer $access_token"
		);

		$url = "https://api.powerbi.com/v1.0/myorg/groups/$workspace/reports/$ReportId/GenerateToken";

		$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;
		}

 

 

Added application as (service principal).
Issued permissions.
Added a service principal to Power Bi Workspace Access.

 

Screenshot_1.pngScreenshot_3.pngScreenshot_2.png

 

But no matter what I do, no matter how I twist the settings, I always get an error: The requested URL returned error: 403
I'm already crazy about this problem.
Help me 😞

0 REPLIES 0

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.