0

I made this function to make it possible to send announcements to my google home mini but im getting error code 400 (Bad Request) nonstop. I tried using multiple approaches to this.

In Home-Assistant, i have setup my Google Home and can play media through it but whenever i try it with the "google_say" API of Home-Assistant it doesn't work.

I also tried calling the Home-Assistant API over my Phone using an App called "API Client" but i got the same response.

    function ttsGoogleHome(text) {
    var ttsUrl = "http://127.0.0.1:8123/api/services/tts/google_say?api_password=<MY_PASSWORD>"
    var varToken = "<MY_TOKEN>"
    
    var postData = {"entity_id": "media_player.david", "message": `${text}`};
  
      let axiosConfig = {
        headers: {
            'authorization': `Bearer ${varToken}`,
            'Content-Type': 'application/json',
            "Access-Control-Allow-Origin": "*",
        }
      };
  
      axios.post(ttsUrl, postData, axiosConfig)
      .then((res) => {
        console.log("RESPONSE RECEIVED: ", JSON.stringify(res));
      })
      .catch((err) => {
        console.log("AXIOS ERROR: ", JSON.stringify(err));
      })
    }

This is the response i get in the server:

        {
          "message": "Request failed with status code 400",
          "name": "Error",
          "stack": "Error: Request failed with status code 400\n    
    at createError (/home/pi/nodejs/node_modules/axios/lib/core/createError.js:16:15)\n    
    at settle (/home/pi/nodejs/node_modules/axios/lib/core/settle.js:17:12)\n    
    at IncomingMessage.handleStreamEnd (/home/pi/nodejs/node_modules/axios/lib/adapters/http.js:260:11)\n    
    at IncomingMessage.emit (events.js:388:22)\n    
    at endReadableNT (internal/streams/readable.js:1336:12)\n    
    at processTicksAndRejections (internal/process/task_queues.js:82:21)",
          "config": {
            "url": "http://127.0.0.1:8123/api/services/tts/google_say?api_password=<MY_PASSWORD>",
            "method": "post",
            "data": "{\"entity_id\":\"media_player.david\",\"message\":\"Erste Stunde  Fach Deutsch Lehrer Schemmer Raum Schemmer\"}",
            "headers": {
              "Accept": "application/json, text/plain, */*",
              "Content-Type": "application/json;charset=UTF-8",
              "authorization": "Bearer <MY_TOKEN>",
              "Access-Control-Allow-Origin": "*",
              "User-Agent": "axios/0.21.1",
              "Content-Length": 103
            },
            "transformRequest": [
              null
            ],
            "transformResponse": [
              null
            ],
            "timeout": 0,
            "xsrfCookieName": "XSRF-TOKEN",
            "xsrfHeaderName": "X-XSRF-TOKEN",
            "maxContentLength": -1,
            "maxBodyLength": -1
          }
        }
Amenofisch
  • 11
  • 4

1 Answers1

1

I found my error.

I used the wrong api link

here is the correct way to call it.

function ttsGoogleHome(text) {
var ttsUrl = "http://127.0.0.1:8123/api/services/tts/google_translate_say?api_password=APIPASSWORD"
var varToken = "TOKEN"

var postData = `{"entity_id": "media_player.david", "message": "${text}", "language": "de"}`;

  let axiosConfig = {
    data: null,
    headers: {
        'authorization': `Bearer ${varToken}`,
        'Content-Type': 'application/json',
        "Access-Control-Allow-Origin": "*",
    }
  };

  axios.post(ttsUrl, postData, axiosConfig)
  .then((res) => {
      console.clear();
    console.info("RESPONSE RECEIVED: ", JSON.stringify(res));
  })
  .catch((err) => {
      console.clear();
    console.error("AXIOS ERROR: ", JSON.stringify(err));
  })

}

also here is my configuration.yaml:

# Configure a default steup of Home Assistant (frontend, api, etc)

# Text to speech
tts:
  - platform: google_translate
  - language: "de"
  - service_name: google_say
  -base_url: http://192.168.0.176:8123
group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

homeassistant:
  auth_providers:
   - type: legacy_api_password
     api_password: !secret http_password
Amenofisch
  • 11
  • 4