NEWS
[Gelöst]Umstellung Request auf Axios
-
Kann mir jemand helfen, wo ich den Denkfehler mache?
Das Skript versucht sich mit dem Websocket der Gardena Smart Api zu verbinden.
Ich versuche gerade das Skript von REQUEST auf AXIOS umzustellen
Mein "Problem" ist dass der Axios immer einen 401 bringt.Request (funktioniert)
request({ url: SMART_HOST+'/v1/websocket', method: 'POST', json: payload, headers: headers } , function (err, response, body) { if (debug) console.log ("response: " + JSON.stringify(response)); try{ ws_url = body.data.attributes.url; console.log ("WS-Url aquired => "+ ws_url); wss = new WebSocket(ws_url); listen_WS(); } catch (error){ console.warn(error) } } );
Axios Fehler 401 (Unauthorized)
axios.post(SMART_HOST+'/v1/websocket', payload, headers) .then((response) => { if (debug) console.log ("response: " + JSON.stringify(response)); ws_url = response.data.attributes.url; console.log ("WS-Url aquired => "+ ws_url); wss = new WebSocket(ws_url); listen_WS(); }) .catch((error) => { if (error.response) { console.warn('error data: ' + JSON.stringify(error.response.data)); console.warn('error status: ' + error.response.status); console.warn('error headers: ' + JSON.stringify(error.response.headers)); } console.warn('error.config: ' + JSON.stringify(error.config)); throw new Error ('Not able to fetch WS-Url: ' + error); });
Der Payload:
let payload = { "data": { "type": "WEBSOCKET", "attributes": { "locationId": location_id }, "id": "request-12312" } };
Und die Headers:
headers ={ 'accept': 'application/vnd.api+json', 'Content-Type': 'application/vnd.api+json', 'Authorization': 'Bearer ' + access_token, 'X-Api-Key': appkey };
javascript.0 14:30:35.673 warn script.js.common.Bewässerung.Gardena_Gateway: error data: {"message":"Unauthorized"} javascript.0 14:30:35.673 warn script.js.common.Bewässerung.Gardena_Gateway: error status: 401 javascript.0 14:30:35.674 warn script.js.common.Bewässerung.Gardena_Gateway: error headers: {"content-type":"application/json","content-length":"26","connection":"keep-alive","date":"Sun, 30 Jun 2024 12:30:35 GMT","x-amzn-requestid":"059e2f54-bb4d-437b-a8fe-2d5befec56d7","x-amzn-errortype":"UnauthorizedException","x-amz-apigw-id":"aLls2FbGjoEEbAA=","x-cache":"Error from cloudfront","via":"1.1 b96dc0b769a91a3fe5483b063383b1c8.cloudfront.net (CloudFront)","x-amz-cf-pop":"AMS58-P4","x-amz-cf-id":"41BeXmjbkUOQvwpm_C5AJukyFe7zJllXB06KQtR6hjvFfn-1byXBoQ=="} javascript.0 14:30:35.674 warn script.js.common.Bewässerung.Gardena_Gateway: error.config: {"transitional":{"silentJSONParsing":true,"forcedJSONParsing":true,"clarifyTimeoutError":false},"adapter":["xhr","http","fetch"],"transformRequest":[null],"transformResponse":[null],"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"maxBodyLength":-1,"env":{},"headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json","User-Agent":"axios/1.7.2","Content-Length":"117","Accept-Encoding":"gzip, compress, deflate, br"},"accept":"application/vnd.api+json","Content-Type":"application/vnd.api+json","Authorization":"Bearer **GEHEIM**","X-Api-Key":"**GEHEIM**","method":"post","url":"https://api.smart.gardena.dev/v1/websocket","data":"{\"data\":{\"type\":\"WEBSOCKET\",\"attributes\":{\"locationId\":\"**GEHEIM**\"},\"id\":\"request-12312\"}}"}
-
axios.post(SMART_HOST + '/v1/websocket', payload, { headers: headers }) .then((response) => { ....
-
@mcu
Vielen Dank, du bist mein Held!