cnk700i / havcs

Home Assistant Voice Control Skill
https://ljr.im/articles/plugins-havcs-edible-instructions/
Apache License 2.0
311 stars 97 forks source link

接入小度授权成功后,30分钟又需要重新授权问题 #52

Open yearnfar opened 2 years ago

yearnfar commented 2 years ago

HomeAssitant版本是:2022.6.6 HAVCS插件设置如下:

{
    "platform": [
        "dueros"
    ],
    "http": {
        "clients": {
            "duerosdemo": "xxxx"
        },
        "ha_url": "http://127.0.0.1:8123",
        "expire_in_hours": 87600
    },
    "device_config": "ui"
}

问题描述

小度授权成功后,30分钟左右提示需要重新登录,重新授权后正常。 重启授权的时候,打开日志后发现报错:

jwt.InvalidTokenError

并且日志有打印
[util] access_token[xxxxxxxx] is invalid, try another reauthorization on website

最终确定是havcs/util.py 第 98行 jwt.decode 方法报错,改为下面写法后正常:

       # 原报错代码
       #unverif_claims = jwt.decode(access_token, verify=False) 

      # 修复代码,注意代码对齐 
       unverif_claims = jwt.decode(access_token, options={"verify_signature": False})

重启HomeAssistant后正常,过期时间是expire_in_hours 设置的时间。

验证方式

查看home-assistant/.storage/auth文件,在refresh_tokens里面可以看到小度的授权信息。 查看 access_token_expiration 时长(单位秒)

            {
                "id": "xxxxx",
                "user_id": "xxxxx",
                "client_id": "https://xiaodu.baidu.com",
                "client_name": null,
                "client_icon": null,
                "token_type": "normal",
                "created_at": "2022-06-23T18:02:42.880327+00:00",
                "access_token_expiration": 315360000.0,
                "token": "xxxx",
                "jwt_key": "xxxxx",
                "last_used_at": "2022-06-23T18:02:42.889879+00:00",
                "last_used_ip": "127.0.0.1",
                "credential_id": "xxxxx",
                "version": "2022.6.6"
            },

关于调试

顺便提一下开启debug模式方法: 修改配置文件configuration.yaml,增加下面四行代码

logger:
  default: error
  logs:
    custom_components.havcs: debug