abbrechen
Suchergebnisse werden angezeigt für 
Anzeigen  nur  | Stattdessen suchen nach 
Meintest du: 

To help get started.

Hi,

A simple practice to "get started".

Regards,

 

 

<?php

// Initialization 
//
$authorizeURL = "https://iam.viessmann.com/idp/v2/authorize";
$tokenURL = "https://iam.viessmann.com/idp/v2/token";
$client_id = "Your_Customer_ID"; // API Key
$code_challenge = "47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU";
$callback_uri = "http://localhost:4200/";
$user = "Your_Email"; // The same as used for Vicare 
$pwd = "Your_Password"; // The same as used for Vicare 

// Code parameters 
//
$url = "$authorizeURL?client_id=$client_id&code_challenge=$code_challenge&scope=IoT%20User&redirect_uri=$callback_uri&response_type=code";
$header = array("Content-Type: application/x-www-form-urlencoded");

$curloptions = array(
    CURLOPT_URL => $url,
    CURLOPT_HTTPHEADER => $header,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_USERPWD => "$user:$pwd",
    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
    CURLOPT_POST => true,
);

// Call Curl Code 
//
$curl = curl_init();
curl_setopt_array($curl, $curloptions);
$response = curl_exec($curl);
curl_close($curl);

// Code Extraction 
//
$matches = array();
$pattern = '/code=(.*)"/';
if (preg_match_all($pattern, $response, $matches)) {
    $code = $matches[1][0];
} else {
    exit("Erreur"."\n");
}

// Token Settings 
//
$url = "$tokenURL?grant_type=authorization_code&code_verifier=$code_challenge&client_id=$client_id&redirect_uri=$callback_uri&code=$code";
$header = array("Content-Type: application/x-www-form-urlencoded");

$curloptions = array(
    CURLOPT_URL => $url,
    CURLOPT_HTTPHEADER => $header,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
    CURLOPT_POST => true,
);

// Call Curl Token 
//
$curl = curl_init();
curl_setopt_array($curl, $curloptions);
$response = curl_exec($curl);
curl_close($curl);

// Token extraction 
//
$json = json_decode($response, true);
$token = $json['access_token'];

// Read user data 
//
$url = "https://api.viessmann.com/users/v1/users/me?sections=identity";
$header = array("Authorization: Bearer $token");

$curloptions = array(
    CURLOPT_URL => $url,
    CURLOPT_HTTPHEADER => $header,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
);

// Data Curl Call 
//
$curl = curl_init();
curl_setopt_array($curl, $curloptions);
$response = curl_exec($curl);
curl_close($curl);

echo($response);

 

14 ANTWORTEN 14

@nerixs , Thanks a lot for sharing a sample code to support others getting started and doing their first API call!

Much appreciated!

Hi,

thanks alot for your sample code. But I am struggling with the easiest things... what exactly is the Client_ID? On my PT2 I can find S/N, ID, S/N from com module... nothing worked.

Do I need to register somewhere to get it?

 

Best regards,

Andi

 

Hi,

Thanks for the code but i am not able to get it run... this Code still works today or did viessmann any changes?

 

Steven

Hi @Steven_G , I quickly checked the code. The only thing that changed should be that we recommend using the v3 of the IDP endpoints. This means you should replace the first to URLs with the following

https://iam.viessmann.com/idp/v3/authorize

https://iam.viessmann.com/idp/v3/token

Please try it out. In case you still get an error, please post the error here so it is easier to find a solution.

Regards,

Michael

Hello again,

 

i tried it with the v3 changes but it still dont work.

I just copied the complete code, edited the $client_id, $user and $pwd part and run it. The response is: "Erreur".

No further Error description or something...

 

Do i have to change another thing in the code? What about the $code_challenge variable?

 

Best regards

@Steven_G the code verifier / code challenge is a good hint. You could try and use the values from the example in our API Documentation or use this tool to create a code verifier / challenge pair.

Also, it would make sense to add the additional query parameter &code_challenge_method=S256 to the first call (see our documentation).

Regards,

Michael

Thanks for your help.

from the first call i get an code, the second call to the endpoint tokenURL return

{"error":"invalid_grant","error_description":"Invalid grant"}

and the third endpoint (v1/users/me) unauthorized 

Please note that the code from the first API call is only valid for 20 seconds. Therefore, you would need to be rather quick in sending the second API call including the code from the first one.

But i use the Code from above so this should not be the Problem i guess... ?

Sorry, I thought you were trying the Postman Collection to test the API calls. In this case, you are right the script will do the second call accordingly.

I noticed that in the second call, the code challenge is also being used as the code verifier. Therefore, two adjustments in the script need to be done:

1) Change the second call to $url = "$tokenURL?grant_type=authorization_code&code_verifier=$code_verifier&client_id=$client_id&redirect_uri=$callback_uri&code=$code";
2) Introduce a new variable in the beginning: $code_verifier = "YOUR CODE VERIFIER"

For adjustment 2), you need to put in the code verifier you created via the tool or use the values from our API documentation (see one of my previous posts)

Please try it and let me know if this works.

It works, thank you very much!

Hi nerixs,

 

I recently started using Viessmann APIs to get information about my Viessmann devices. Your sample code was really a great support to get my first code running properly - thanks for that!

 

As I did not yet completely understand the token handling I have the following question:

you are using the code 

$json = json_decode($response, true);
$token = $json['access_token'];

the get the token from the token call. But I cannot see any line in your code where the variable $token is used. Can you give me hint?

 

Regards Winfried

Please ignore my question above - I got it 😉