家庭助理 - 请求失败错误代码 400
Posted
技术标签:
【中文标题】家庭助理 - 请求失败错误代码 400【英文标题】:Home Assistant - Request failed Error code 400 【发布时间】:2021-07-15 13:45:24 【问题描述】:我制作这个功能是为了可以向我的 google home mini 发送通知,但我不停地收到错误代码 400(错误请求)。我尝试使用多种方法。
在 Home-Assistant 中,我已经设置了我的 Google Home,并且可以通过它播放媒体,但是每当我使用 Home-Assistant 的“google_say”API 进行尝试时,它都不起作用。
我还尝试使用名为“API 客户端”的应用程序通过手机调用 Home-Assistant API,但得到了相同的响应。
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));
)
这是我在服务器中得到的响应:
"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
【问题讨论】:
【参考方案1】:我发现了我的错误。
我用错了api链接
这是正确的调用方式。
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));
)
这里也是我的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
【讨论】:
以上是关于家庭助理 - 请求失败错误代码 400的主要内容,如果未能解决你的问题,请参考以下文章