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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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