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
Zuv
Frequent Visitor

Bad token (probably) on rest api call to Patch GatewayDatasource (403 Forbidden error)

Hello guys,

I am trying to update datasource credentials of dataset, first I am getting response of "Get GetBoundGatewayDatasources" with ID and Gateway id without any problems
Later on I am calling to "Patch GatewayDatasource" without any luck. I am getting 403 Forbidden error.
All the call I used before allowed me to use AppKey instead of token, unfortunately "Patch GatewayDatasource" seems like allows only tokens, because when I do use AppKey I am getting error that Token is not detected on Json, newerthless they tell us AppKey or Token is allowed: https://msdn.microsoft.com/en-us/library/azure/mt711498.aspx

This is how I am getting token, and I do believe something is wrong here:

public function getDashboardToken($workspaceId,$reportId) {

		$key = env('ACCESS_KEY');
		$collection_name = env('WORKSPACE_COLLECTION');
                $payload = [
                    "ver" => "0.2.0",
                    "type" => "embed",
                    "wcn" => $collection_name,
                    "wid" => $workspaceId,
                    "rid" => $reportId,
                    "iss" => "PowerBISDK",
                    "aud" => "https://analysis.windows.net/powerbi/api",
                    "exp" => time()+60*60,
                    "nbf" => time()
                ];
						
                $tokenListing = JWT::encode($payload, $key);
                return ($tokenListing);
		}

This is my method for "Patch GatewayDatasource":

$ch = curl_init();

$data = array('credentialType' => 'Basic',
			'basicCredentials'=>array(
						'username' => 'username',
						'password' => 'password',
					)
			);
					
$token = $this->getDashboardToken($workspaceId, $datasetId);

$headers = array();
$headers[] = "Authorization: AppToken " . $token;
$headers[] = 'Content-Type: application/json; charset=utf-8';

curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_URL, "https://api.powerbi.com/v1.0/collections/".$collection_name."/workspaces/".$workspaceId."/gateways/".$gatewayId."/datasources/".$datasource_id."/");

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //Might be required for https
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
            				
$response_json = curl_exec($ch);

$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

Many thanks!

1 ACCEPTED SOLUTION
Zuv
Frequent Visitor

Ok, seems like small error - I need to use

 

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

instead of:

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

 

And yeah, I need to use AppKey instead of Token, or at least I can't working it with token.

View solution in original post

1 REPLY 1
Zuv
Frequent Visitor

Ok, seems like small error - I need to use

 

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

instead of:

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

 

And yeah, I need to use AppKey instead of Token, or at least I can't working it with token.

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.