# API Ecowatt via Node-Red vers Gladys

**URL:** <https://community.gladysassistant.com/t/api-ecowatt-via-node-red-to-gladys/7558>\
**Category:** Random\
**Tags:** node-red\
**Created:** [Octobre 2, 2022, 12:55 UTC](https://community.gladysassistant.com/t/api-ecowatt-via-node-red-to-gladys/7558 "2022-10-02T12:55:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jparbel](https://gladys-assistant-community.b-cdn.net/letter_avatar_proxy/v4/letter/j/d6d6ee/32.png) [@jparbel](https://community.gladysassistant.com/u/jparbel)\
**Post date:** [Octobre 2, 2022, 12:55 UTC](https://community.gladysassistant.com/t/api-ecowatt-via-node-red-to-gladys/7558/1 "2022-10-02T12:55:15Z")

</div>

Je souhaiterais intégrer la nouvelle API Ecowatt ([Catalogue API - API Data RTE](https://data.rte-france.com/catalog/-/api/doc/user-guide/Ecowatt/4.0)) dans Gladys.  
Cette API est accessible via un bearer token éphémère qu’il faut renouveler toutes les 2 heures.

En passant par Node-red on peut récupérer le token grâce à une requête http, puis réinjecter manuellement ce token dans une nouvelle requête http pour obtenir le payload de l’API.  
Cette solution marche bien mais n’est pas très pratique !

J’ai donc cherché (sans succès) une solution pour automatiser cette 2ième requête en injectant une variable correspondant au token actualisé toutes les 2 heures.  
Je pense qu’il faut passer par une fonction mais je n’arrive pas à fournir les bons headers de la requête.

Si un spécialiste Node-red a la solution, je suis preneur.  
Merci d’avance.  
jparbel

---

<div class="post-metadata">

**Author:** ![VonOx](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/vonox/32/10128_2.png) [@VonOx](https://community.gladysassistant.com/u/VonOx)\
**Post date:** [Octobre 3, 2022, 4:19 UTC](https://community.gladysassistant.com/t/api-ecowatt-via-node-red-to-gladys/7558/2 "2022-10-03T04:19:52Z")

</div>

C’est une authentification oauth2, y’a des palettes pour gérer ce protocole.

EDIT: Je viens de tester ça avec succès

 ![image](https://gladys-assistant-community.b-cdn.net/uploads/default/original/2X/a/a8440ed8a41d7b2f2f11c7ee0778b56ca2071100.png)

---

<div class="post-metadata">

**Author:** ![VonOx](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/vonox/32/10128_2.png) [@VonOx](https://community.gladysassistant.com/u/VonOx)\
**Post date:** [Octobre 3, 2022, 7:10 UTC](https://community.gladysassistant.com/t/api-ecowatt-via-node-red-to-gladys/7558/3 "2022-10-03T07:10:44Z")

</div>

Un flow d’exemple

```json
[
    {
        "id": "eee1757110226f86",
        "type": "tab",
        "label": "Ecowatts -> Gladys",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "27ed541908cb256d",
        "type": "oauth2",
        "z": "eee1757110226f86",
        "name": "",
        "container": "oauth2Response",
        "grant_type": "client_credentials",
        "access_token_url": "https://digital.iservices.rte-france.com/token/oauth/",
        "authorization_endpoint": "",
        "redirect_uri": "/oauth2/redirect_uri",
        "open_authentication": "",
        "username": "",
        "password": "",
        "client_id": "XXXXXXXXXXXXXXXXXXX",
        "client_secret": "XXXXXXXXXXXXXXXX",
        "scope": "",
        "rejectUnauthorized": true,
        "headers": {},
        "x": 250,
        "y": 60,
        "wires": [
            [
                "e54c33cdc4a66e73"
            ]
        ]
    },
    {
        "id": "5c8888796344fe7e",
        "type": "debug",
        "z": "eee1757110226f86",
        "name": "debug 2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 680,
        "y": 60,
        "wires": []
    },
    {
        "id": "4141ecf227805b07",
        "type": "inject",
        "z": "eee1757110226f86",
        "name": "Get",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 110,
        "y": 60,
        "wires": [
            [
                "27ed541908cb256d"
            ]
        ]
    },
    {
        "id": "d1cc9455efc67f83",
        "type": "http request",
        "z": "eee1757110226f86",
        "name": "",
        "method": "GET",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "https://digital.iservices.rte-france.com/open_api/ecowatt/v4/signals",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 530,
        "y": 60,
        "wires": [
            [
                "5c8888796344fe7e"
            ]
        ]
    },
    {
        "id": "e54c33cdc4a66e73",
        "type": "function",
        "z": "eee1757110226f86",
        "name": "function 1",
        "func": "msg.headers = {\n Authorization: \"Bearer \"+msg.oauth2Response.access_token\n}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 380,
        "y": 60,
        "wires": [
            [
                "d1cc9455efc67f83"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![jparbel](https://gladys-assistant-community.b-cdn.net/letter_avatar_proxy/v4/letter/j/d6d6ee/32.png) [@jparbel](https://community.gladysassistant.com/u/jparbel)\
**Post date:** [Octobre 3, 2022, 11:05 UTC](https://community.gladysassistant.com/t/api-ecowatt-via-node-red-to-gladys/7558/4 "2022-10-03T11:05:44Z")

</div>

@VonOx, merci Vincent, ça marche super bien, c’est pas compliqué quand on sait faire !!
