PicoC programing Oauth2 ttlock

Einklappen
X
 
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge
  • eIRIk
    Dumb Home'r
    • 27.03.2018
    • 11

    #1

    PicoC programing Oauth2 ttlock

    Hi
    I'm creating a program for oauth2 for ttlock and i have problem reading out a value from "expires_in".


    Here is the replay from the request:

    Code:
    {"access_token":"815ef380f7345a76357026c69a71c1ba" ,"refresh_token":"b09efe6945ergd762a5a5a1eaa4c1aba ","uid":7488719,"openid":1785435683,"scope":"u ser, key,room","token_type":"Bearer","expires_in":2779073}
    The token and refresh tokens extract are working fine. But the value from expires_in, is not working. I have used all text outputs so it needs to be a numberic value sent to setoutput to be used.


    // Extracting refresh_token
    char* refresh_token = strstrskip(buffer, ""refresh_token":"");
    int pos_B = strfind(refresh_token, """, 0);
    refresh_token[pos_B] = 0;
    setoutputtext(1, refresh_token);


    // Extracting access_token
    char* access_token = strstrskip(buffer, ""access_token":"");
    int pos_A = strfind(access_token, """, 0);
    access_token[pos_A] = 0;
    setoutputtext(0, access_token);


    // Extracting expires_in
    char* expires_in_start = strstrskip(buffer, ""expires_in":");
    if (expires_in_start != NULL) {
    expires_in_start += strlen(""expires_in":");
    double expires = atof(expires_in_start);
    printf("Expires in seconds: %f\n", expires);
    int expires_int = (int)expires;
    setoutput(0, expires_int);

    Br eirik​
    Zuletzt geändert von eIRIk; 25.03.2024, 07:17.
    Miniserves v1 & v2 v14.7.3.6, Test Miniserver v1, Relay Ext., 7xDimmer Ext., 1-Wire Ext. Modbus ext., Air Ext, RS485 Ext. 8xRGBW Dimmer Ext, Tree Ext., 2xtree to air, Wether station, KebaContact P20, Intercom +++ Loxberry: MiRobot2Lox, TinyTuya Ventilation: System Air via Modbus RTU AMS: PowU, HomeConnect, PowerWiew Shading, Husqvarna Automover
  • Labmaster
    Lox Guru
    • 20.01.2017
    • 2696

    #2
    Also wenn man mal diese komische "73" von "27790 73" ignorieren würde, dann sollte der folgende Code funktionieren.


    Code:
    // Extracting expires_in
    char* expires_in_start = strstr(buffer, "\"expires_in\":");
    if (expires_in_start != NULL) {
    expires_in_start += strlen("\"expires_in\":");
    double expires = atof(expires_in_start);
    printf("Expires in seconds: %f\n", expires);
    int expires_int = (int)expires;
    setoutput(0, expires_int);
    }​

    Output for

    buffer = {"access_token":"815ef380f7345a76357026c69a71c1 ba" ,"refresh_token":"b09efe6945ergd762a5a5a1eaa4c1 aba ","uid":7488719,"openid":1785435683,"scope":"u ser, key,room","token_type":"Bearer","expires_in":27790 73}

    is

    Expires in seconds: 27790.000000​

    Kommentar

    • Jan W.
      Lox Guru
      • 30.08.2015
      • 1496

      #3
      A believe that the order needs to be changed, because you are modifying the original buffer with zeros (end of string symbols). The first part of the code is only working, because "refresh_token" is following "access_token" in the json file, so when the " at the end of refresh_token is replaced by a 0, you are still able to find the access_token. The last token "exprires_in" can't be found anymore, because the end of the string (buffer) is now at the end of the access token.

      Simple solution: put the third part "extracting expires_in" to the top of the code. A better solution might be to move the last two statements of the first two blocks to the end of your code, so that the code still works if the order of the elements in the json files is changed.
      Miniserver v14.5.12.7, 2x Ext., 2x Relay Ext., 2x Dimmer Ext., DMX Ext., 1-Wire Ext., Gira KNX Tastsensor 3 Komfort, Gira KNX Präsenzmelder, Fenster- und Türkontakte, Loxone Regen- und Windsensor, Gira Dual Q Rauchmelder vernetzt, 1x Relais-Modul
      Loxberry: SmartMeter, MS Backup, CamConnect, Weather4Lox
      Lüftung: Helios KWL EC 370W ET mit Modbus TCP - via Pico-C
      Heizung: Stiebel Eltron WPF 5 cool (Sole-Wasser WP) mit ISG, FB-Heizung mit 18 Kreisen, Erdsonde - via modbus/TCP
      Node-RED: IKEA Tradfri

      Kommentar

      • eIRIk
        Dumb Home'r
        • 27.03.2018
        • 11

        #4
        Hi
        First of all the json string don't have the space betveen 0 and 7 it's just a value 7 digit. like expires_in":2779073
        Pasting this in the forum make these strange spaces. Tryed to edit, but space still come back.

        Anyway, I have chagged the code as Jan W. preposed expires in to the top, and moved the setoutput to the bottom.

        But still the value is not shown in O1. Any other solutions....?? Or any better ways to do this extraction?


        stream_close(stream);

        // Extracting expires_in
        char* expires_in_start = strstrskip(buffer, ""expires_in":");
        if (expires_in_start != NULL) {
        expires_in_start += strlen(""expires_in":");
        double expires = atof(expires_in_start);
        printf("Expires in seconds: %f\n", expires);
        int expires_int = (int)expires;
        setoutput(0, expires_int);
        }

        // Extracting refresh_token
        char* refresh_token = strstrskip(buffer, ""refresh_token":"");
        int pos_B = strfind(refresh_token, """, 0);


        // Extracting access_token
        char* access_token = strstrskip(buffer, ""access_token":"");
        int pos_A = strfind(access_token, """, 0);


        refresh_token[pos_B] = 0;
        setoutputtext(1, refresh_token);
        access_token[pos_A] = 0;
        setoutputtext(0, access_token);
        setoutputtext(2, client_id);
        setoutput(1, lock_id);​
        Zuletzt geändert von eIRIk; 24.03.2024, 22:37.
        Miniserves v1 & v2 v14.7.3.6, Test Miniserver v1, Relay Ext., 7xDimmer Ext., 1-Wire Ext. Modbus ext., Air Ext, RS485 Ext. 8xRGBW Dimmer Ext, Tree Ext., 2xtree to air, Wether station, KebaContact P20, Intercom +++ Loxberry: MiRobot2Lox, TinyTuya Ventilation: System Air via Modbus RTU AMS: PowU, HomeConnect, PowerWiew Shading, Husqvarna Automover

        Kommentar

        • Labmaster
          Lox Guru
          • 20.01.2017
          • 2696

          #5
          Have you tested the code i posted above ?

          As i wrote , this will work a mentioned above (i tested it), especially if the space in between the two numbers is in real not there.

          Hint:
          if you post program code i a forum like this, use the
          [CODE ] HERE PROGRAM CODE [/CODE ] Tags around it. (no space inside the code tag, i only inserted it here to not get the tag active)
          Zuletzt geändert von Labmaster; 25.03.2024, 06:06.

          Kommentar

          • eIRIk
            Dumb Home'r
            • 27.03.2018
            • 11

            #6
            Hi, yes i tested your code Labmaster but still show 0 on O1.
            I might think there is someting wrong with the stream. the json string above is copyed from postman.
            If you can confirm that the code you posted is working, then it has to be something with the stream.

            Maybe I’m not recieving the complete json string?

            char buffer[65536];
            int length = strlen(client_id) + strlen(client_secret) + strlen(username) + strlen(password) + 45;

            STREAM* stream = stream_create("/dev/tcp/euapi.ttlock.com/80", 0, 0);
            char* cmd = "POST /oauth2/token HTTP/1.1\r\nHost: euapi.ttlock.com\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\n";
            sprintf(buffer, cmd, length);

            stream_write(stream, buffer, strlen(buffer));
            sprintf(buffer, "client_id=%s&client_secret=%s&u
            sername=%s&passwor d=%s\r\n", client_id, client_secret, username, password);
            stream_write(stream, buffer, strlen(buffer));
            stream_flush(stream);

            int cnt = 0;
            int total = 0;
            do
            {
            cnt = stream_read(stream, (char*)(((int)(char*)buffer)+total), 65536 - total, 5000);
            total += cnt;
            }
            while (cnt > 0 && total < 65536);

            stream_close(stream);​
            Zuletzt geändert von eIRIk; 25.03.2024, 07:45.
            Miniserves v1 & v2 v14.7.3.6, Test Miniserver v1, Relay Ext., 7xDimmer Ext., 1-Wire Ext. Modbus ext., Air Ext, RS485 Ext. 8xRGBW Dimmer Ext, Tree Ext., 2xtree to air, Wether station, KebaContact P20, Intercom +++ Loxberry: MiRobot2Lox, TinyTuya Ventilation: System Air via Modbus RTU AMS: PowU, HomeConnect, PowerWiew Shading, Husqvarna Automover

            Kommentar

            • Labmaster
              Lox Guru
              • 20.01.2017
              • 2696

              #7
              i tested it here:



              full code there was:
              Code:
              #include <stdio.h>
              #include <stdlib.h>
              #include <string.h>
              
              char *buffer = "{\"access_token\":\"815ef380f7345a76357026c69a71c1ba\" ,\"refresh_token\":\"b09efe6945ergd762a5a5a1eaa4c1aba \",\"uid\":7488719,\"openid\":1785435683,\"scope\":\"user, key,room\",\"token_type\":\"Bearer\",\"expires_in\":27790 73}";
              
              void main() {
                 printf("%s\n\n", buffer);
              
              // Extracting expires_in
              char* expires_in_start = strstr(buffer, "\"expires_in\":");
              if (expires_in_start != NULL) {
              expires_in_start += strlen("\"expires_in\":");
              double expires = atof(expires_in_start);
              printf("Expires in seconds: %f\n", expires);
              int expires_int = (int)expires;
              
              printf("----------\n");
              //setoutput(0, expires_int);
              }
              }​

              Kommentar

              • eIRIk
                Dumb Home'r
                • 27.03.2018
                • 11

                #8
                Labmaster I got this error testing your code: file.c:21:2 illegal character '⧀

                What do i do wrong?

                BR eIRIk
                Miniserves v1 & v2 v14.7.3.6, Test Miniserver v1, Relay Ext., 7xDimmer Ext., 1-Wire Ext. Modbus ext., Air Ext, RS485 Ext. 8xRGBW Dimmer Ext, Tree Ext., 2xtree to air, Wether station, KebaContact P20, Intercom +++ Loxberry: MiRobot2Lox, TinyTuya Ventilation: System Air via Modbus RTU AMS: PowU, HomeConnect, PowerWiew Shading, Husqvarna Automover

                Kommentar


                • Jan W.
                  Jan W. kommentierte
                  Kommentar bearbeiten
                  Just delete the last Char and insert a bracket } manually.
              • eIRIk
                Dumb Home'r
                • 27.03.2018
                • 11

                #9
                Hi
                Yes know it's working. I changed the printf("Expires in seconds: %f\n", expires); to printf("Expires in seconds: %d\n", (int)expires); to remove the .00000.

                Know it's just to find out why the complete script dosent give me this replay.

                BR eIRIk
                Miniserves v1 & v2 v14.7.3.6, Test Miniserver v1, Relay Ext., 7xDimmer Ext., 1-Wire Ext. Modbus ext., Air Ext, RS485 Ext. 8xRGBW Dimmer Ext, Tree Ext., 2xtree to air, Wether station, KebaContact P20, Intercom +++ Loxberry: MiRobot2Lox, TinyTuya Ventilation: System Air via Modbus RTU AMS: PowU, HomeConnect, PowerWiew Shading, Husqvarna Automover

                Kommentar

                • Jan W.
                  Lox Guru
                  • 30.08.2015
                  • 1496

                  #10
                  It might be worth to use Wireshark to verify if there are any special characters in the token string, e.g. spaces, return or something else. See https://loxwiki.atlassian.net/wiki/s...er+im+Netzwerk (it's German, so use Google Translate).
                  Miniserver v14.5.12.7, 2x Ext., 2x Relay Ext., 2x Dimmer Ext., DMX Ext., 1-Wire Ext., Gira KNX Tastsensor 3 Komfort, Gira KNX Präsenzmelder, Fenster- und Türkontakte, Loxone Regen- und Windsensor, Gira Dual Q Rauchmelder vernetzt, 1x Relais-Modul
                  Loxberry: SmartMeter, MS Backup, CamConnect, Weather4Lox
                  Lüftung: Helios KWL EC 370W ET mit Modbus TCP - via Pico-C
                  Heizung: Stiebel Eltron WPF 5 cool (Sole-Wasser WP) mit ISG, FB-Heizung mit 18 Kreisen, Erdsonde - via modbus/TCP
                  Node-RED: IKEA Tradfri

                  Kommentar

                  • Labmaster
                    Lox Guru
                    • 20.01.2017
                    • 2696

                    #11
                    have you tried:

                    Code:
                    ...
                    double expires = atof(expires_in_start);
                    setoutput(0, expires);​
                    ...

                    Kommentar

                    • sagos
                      Dumb Home'r
                      • 08.02.2020
                      • 18

                      #12
                      Loxone Library TTLock https://library.loxone.com/detail/ttlock-1785/overview

                      Guten Tag. Hat jemand die Integration mit Txtlock eingerichtet? Ich kann kein Token erhalten
                      1. Add all locks and gateways into the app.
                      2. Ensure that you can use your mobile app to remotely control your door lock.
                      3. Register a developer account on the door lock supplier's website.(Sciener Open Platform)
                      4. Once your developer account is approved, you will receive a "client_id" and "client_secret."
                      5. Edit the Program code and set the client_id, client_secret, username and password parameters. Password must be md5 encrypted. (MD5 Hash Generator)
                      6. Add this into the PicoC program in TTLock example file.
                      7. Then you will obtain an access token and refresh token.
                      8. Access token returned will expire in seconds of "expires_in"(Default validity of 90 days),request with expired access token will get 10004 error code.
                      Das Skript wird nicht ausgeführt
                      Angehängte Dateien
                      Zuletzt geändert von sagos; 18.06.2025, 07:17.

                      Kommentar

                      Lädt...