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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
wingmanzz
Regular Visitor

PHP and Curl to get embed token for service principal, returns Bad Requst

Hello All I wrote this code to get an AccessToken for my service principal, then use that accesstoken to call the GenerateToken REST API for the specific report. The AccessToken part works flawlessly--I get back an accesstoken which i can then use to make the GenerateToken call. However, the GenerateToken call returns a 'Bad Request' JSON response:

 

{"error":{"code":"BadRequest","message":"Bad Request","details":[{"message":"Input string '--------------------------7e628d604e6ac9f4' is not a valid number. Path '', line 1, position 42.","target":"request"}]}}

 

The hex after the -----------------always changes with each run.

What could be the issue What have I missed?

Here is the code I used. You will need to define $clientId, $clientSecret, $group, $report and $tenantId if you attempt to run it.



<?php
/* Get token using a POST request */

...
$urlAccessToken = "https://login.windows.net/$tenantId/oauth2/token";
$resource = 'https://analysis.windows.net/powerbi/api';

$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"};
echo $access_token;

$headers = array(
"Content-Type: application/json; charset=utf-8",
"Authorization: Bearer $access_token"
);


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

$payload = json_encode($post_params);

echo $payload;

$ch2 = curl_init( $url );
curl_setopt( $ch2, CURLOPT_POST, true);
curl_setopt( $ch2, CURLINFO_HEADER_OUT, true);
curl_setopt( $ch2, CURLOPT_POSTFIELDS, $post_params);
curl_setopt( $ch2, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch2, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec( $ch2 );
echo $response;


?>

1 ACCEPTED SOLUTION
codegardener
New Member

We ran into the same issue, and the resolution for us was to json_encode the post parameters before sending to Curl. We only needed to do this after getting our access token (ie. in the API calls protected by OAUTH).

 

In your code, it looks like you are json encoding to $payload, but then still sending $post_params in the CURLOPT_POSTFIELDS.

View solution in original post

3 REPLIES 3
codegardener
New Member

We ran into the same issue, and the resolution for us was to json_encode the post parameters before sending to Curl. We only needed to do this after getting our access token (ie. in the API calls protected by OAUTH).

 

In your code, it looks like you are json encoding to $payload, but then still sending $post_params in the CURLOPT_POSTFIELDS.

Thanks--

 

That (embarrassingly) actually fixed that error--im going to accept this as the solution.

Now we are getting a "The requested URL returned error: 404 Not Found" error from the  GenerateToken call. But Ill open a seperate issue for that.

SgtBhaji
Regular Visitor

@wingmanzz  Hey there. Were you able to resolve this issue? I'm facing a similar problem at present. Thanks!

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.