Advance your Data & AI career with 50 days of live learning, dataviz contests, hands-on challenges, study groups & certifications and more!
Get registeredGet Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more
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;
?>
Solved! Go to Solution.
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.
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.
@wingmanzz Hey there. Were you able to resolve this issue? I'm facing a similar problem at present. Thanks!
 
					
				
				
			
		
Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!
Check out the October 2025 Power BI update to learn about new features.
