Cancel
Showing results for 
Search instead for 
Did you mean: 

Authorization problem (PHP)

Experimenter
Experimenter

Hello!

 

I am still in the process of learning alot about how to use mindsphere.

I am writing my apps in PHP.

 

I am working with the basics, how to read time series data.

I havent't really understood how to authorize my app.

 

I have uploaded the app to Cloud Foundry and the app is running, but I cannot get  access to any time series data.

I do not know how to authorize with the token. I have a client_id and a client_secret.

 

How do I add the authorization to this?

 

My code is as below:

 

<?php
$client_id = "client_id"; $client_secret = "client_secret"; $asset = "tenant.assetName"; $aspect = "tenant.aspectName"; $variable = "myVariable"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://gateway.eu1.mindsphere.io/api/iottimeseries/v3/".$asset."/".$aspect."?select=".$variable);
$headers = array( 'X-SPACE-AUTH-KEY: Basic ' . base64_encode($client_id . ":" . $client_secret), 'Content-type: application/json', ); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURL_GET, 1); $response = curl_exec($ch); curl_close($ch);
$result = json_decode($response, true); echo $result;
?>

 

7 REPLIES 7

Re: Authorization problem (PHP)

Pioneer
Pioneer
great that you are doing in PHP.
now tell me u are new in PHP or in MindSphere?
based on that i can help u.

Re: Authorization problem (PHP)

Experimenter
Experimenter

Hi!

 

I have worked in PHP for many years, but I haven't used curl or Oauth before so this is new to me,

but mostly I am new to Mindsphere.

 

I have managet to get the Token, but not the time series data, see updated code below:

 

Updated code below:

<?php
$client_id = "clientid";
$client_secret = "secret";
$app_name = "appname";
$app_version = "1.0.0";
$hostTenant = "tenant";
$userTenant = "tenant";
$asset = "asset";
$aspect = "aspect";
$variables = "Counter"; // Variable name is with capital C

// Get Token ------------------- THIS PART WORKS -----------
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://gateway.eu1.mindsphere.io/api/technicaltokenmanager/v3/oauth/token");
$headers = array(
    'Content-type: application/json',
    'X-SPACE-AUTH-KEY: Basic ' . base64_encode($client_id . ':' . $client_secret),
);
$post_data = 	
"{
    \"appName\":\"".$app_name."\",
    \"appVersion\":\"".$app_version."\",
    \"hostTenant\":\"".$hostTenant."\",
    \"userTenant\":\"".$userTenant."\"
}";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURL_POST, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$raw_token = curl_exec($ch);
curl_close($ch);
$token = json_decode($raw_token, true);
$access_token = $token['access_token'];

// ---------------------------- TOKEN RECIEVED AND SAVED IN $access_token --------------------

// Continue to fetch timeseries data -------------- THIS PART RETURNS NULL ---------------- $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://gateway.eu1.mindsphere.io/api/iottimeseries/v3/".$asset."/".$aspect."?latestValue=true&select=".$variables ); $headers = array( 'Authorization: Bearer '.$access_token, 'Content-type: application/json', ); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); $response = curl_exec($ch); curl_close($ch); $result = json_decode($response, true); echo "Data below: <br> $result <br><br>".var_dump($result); echo "<hr>"; // No data is returned, no error returned. ?>

Re: Authorization problem (PHP)

Pioneer
Pioneer

okay good that you know PHP.
have you tested your api call on postman?
if not please test there.
if testing has been done on postman then the code looks good from my end.
but still i will try to run your code and update you what is the output.

u can ping me on circuit if any othere issues u have
singh.ajit@siemens.com

 

 

Ajit Singh.

Re: Authorization problem (PHP)

Experimenter
Experimenter

Found one problem, had the wrong URL to time series API!

Was: https://gateway.eu1.mindsphere.io/api/iottimeseries/v3/asset/aspect

Should be: https://gateway.eu1.mindsphere.io/api/iottimeseries/v3/timeseries/asset/aspect

 

But now I get error:

[6408] Unable to find the entity - tenant.xxx_variabletest

 

Full response:

array(6) { ["timestamp"]=> string(28) "2019-08-08T08:31:14.413+0000" ["status"]=> int(404) ["error"]=> string(9) "Not Found" ["exception"]=> string(49) "com.siemens.mindsphere.iot.exception.IotException" ["message"]=> string(58) "[6408] Unable to find the entity - tenant.xxx_variabletest" ["path"]=> string(73) "/api/iottimeseries/v3/timeseries/tenant.xxx_variabletest/xxx_variabletest" } 

 

I have masked tenant and xxx from my code.

 

I have tested with both with correct capital letters "tenant.xxx_VariableTest" as they are named in Asset Manager, and lowercase "tenant.xxx_variabletest".

 

Code for reference:

// Get timeseries data

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://gateway.eu1.mindsphere.io/api/iottimeseries/v3/timeseries/".$asset."/".$aspect);
$headers = array(
    'Authorization: Bearer '.$access_token,
    'Content-type: application/json',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);

echo "Data below: <br> <pre>".var_dump($result)."</pre>";
echo "<hr>";

 

 

 

Regards

Daniel

 

Re: Authorization problem (PHP)

Pioneer
Pioneer
Hi Daniel,

i have tested your code it is working fine at my end.
just that im not able to generate access token from your code. but as you are able to get the access token u can do below.

now what u can do is go step by step
1. run the code to get the access_token
2. than copy the access_token and comment crul code of getting access token
3. put the access token in $access_token variable
4. now run the timeseries code to get the result with step 3 access token.

if still not working do let me know.

Re: Authorization problem (PHP)

Pioneer
Pioneer
Hi Daniel,

are you able to get the data from the API?
do let me know in case of any issues.

Regards,
Ajit

Re: Authorization problem (PHP)

Experimenter
Experimenter

Hello!


I got it working! 

If anyone else has problems, this is my working PHP code:

<?php
// Get token for accessing data from backend (PHP script).
// Token validity is returned in array

// Set this to true to enable debugging
$debug = false;

// User data
$client_id = "myClientID";
$client_secret = "myClientSecret";
$app_name = "myApp";
$app_version = "1.0.0";
$hostTenant = "myHostTenant";
$userTenant = "myUserTenant";
$asset = "MyAssetID (ie 145f15xxxxxxxxxxa83e157xxxxxxxxx)";
$aspect = "MyAspectName";

// --- BEGIN -------------------------------- Get security token -----------------------------------
// Initiate cURL session
$ch = curl_init();

// Set cURL URL, URL for requesting Token (from Mindsphere App @ Mindsphere)
curl_setopt($ch, CURLOPT_URL, "https://gateway.eu1.mindsphere.io/api/technicaltokenmanager/v3/oauth/token");

// Set cURL HTTP headers
$headers = array(
    'Content-type: application/json',
    'X-SPACE-AUTH-KEY: Basic ' . base64_encode($client_id . ':' . $client_secret),
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);             // Set headers from variable $headers
curl_setopt($ch, CURLOPT_HEADER, false);                    // Finished with headers
// Set body data (post data)
$post_data = 	
"{
    \"appName\":\"".$app_name."\",
    \"appVersion\":\"".$app_version."\",
    \"hostTenant\":\"".$hostTenant."\",
    \"userTenant\":\"".$userTenant."\"
}";
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);            // Set postfields from variable $post_data;
curl_setopt($ch, CURL_POST, 1);                              // Set mode to POST
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);     
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);              // Enable readable answer from cURL, instead of status
$raw_token = curl_exec($ch);                                 // Execute cURL request, save returned data in $raw_token
curl_close($ch);                                             // Close cURL connection
$token = json_decode($raw_token, true);                      // Decode response from json to array
$access_token = $token['access_token'];                      // Get token from array key 'access token'

if ($debug) echo "DEBUG: Token is: $access_token <br><hr><br>";     // Print token for debug
// --- END -------------------------------- Get securituy token -----------------------------------

// --- BEGIN -------------------------------- Get timeseries data -----------------------------------

// Initiate cURL session
$ch = curl_init();
// Set cURL URL, Get latest values from all variables of aspect $aspect
curl_setopt($ch, CURLOPT_URL, "https://gateway.eu1.mindsphere.io/api/iottimeseries/v3/timeseries/".$asset."/".$aspect); 
// Set cURL HTTP headers
$headers = array(
    'Authorization: Bearer '.$access_token,
    'Content-type: application/json',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);              // Set headers from variable $headers
curl_setopt($ch, CURLOPT_HEADER, false);                     // Finished with headers
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);     
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);              // Enable readable answer from cURL, instead of status
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");              // Set mode to GET (not required in all PHP versions)
$response = curl_exec($ch);                                  // Execute cURL request, save returned data in $raw_token
curl_close($ch);                                             // Close cURL connection
$result = json_decode($response, true);                      // Decode response from json to array

// Print array data if debug mode
if ($debug) {
    echo "DEBUG: Array data below: <br> <pre>";
    print_r ($result);
    echo "</pre><hr>";
}

$var_counter = $result[0]['Counter'];                       // Save value from first data set [0] in response, variable 'Counter' to $var_counter
$var_timer = $result[0]['Timer'];                           // ave value from first data set [0] in response, variable 'Timer' to $var_timer
// --- END -------------------------------- Get timeseries data -----------------------------------