cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
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
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
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

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.

Regular Visitor

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

Helpful resources

Announcements
secondImage

Happy New Year from Power BI

This is a must watch for a message from Power BI!

December Update

Check it Out!

Click here to read more about the December 2020 Updates!

Community Blog

Check it Out!

Click here to read the latest blog and learn more about contributing to the Power BI blog!

Get Ready for Power BI Dev Camp

Get Ready for Power BI Dev Camp

Mark your calendars and join us for our next Power BI Dev Camp!.