如何用Keybase Chat API读取团队聊天?JSON格式报错求助
Keybase Chat API团队查询报错解决方法
首先看你提供的团队聊天查询JSON,存在语法错误:"members_type": "team"后面缺少逗号,这直接导致JSON解析失败,报错里的invalid character 'm' looking for beginning of object key string就是因为解析到topic_name的m时,前面没有逗号分隔,不符合JSON语法规范。
其次,你的Python执行命令存在shell转义问题:用json.dumps生成的JSON包含双引号,直接通过echo传给shell时,shell会把双引号当成命令的一部分解析,破坏了JSON的结构,这也是报错的诱因之一。
解决步骤:
修正JSON语法错误
在"members_type": "team"末尾添加逗号,正确的查询JSON如下:{ "method": "read", "params": { "options": { "channel": { "name": "teamnamehere", "pagination": { "num": 1000 }, "members_type": "team", "topic_name": "channelnamehere" } } } }修复Python命令的shell转义问题
- 方法一:用
shlex.quote对JSON字符串进行转义,避免shell解析双引号:import json import shlex q = json.dumps(query) cmd = "echo {} | keybase chat api > {}".format(shlex.quote(q), json_out) - 方法二:改用
subprocess模块直接传递输入,完全绕过shell解析,更稳定:import json import subprocess with open(json_out, 'w') as output_file: subprocess.run( ["keybase", "chat", "api"], input=json.dumps(query).encode('utf-8'), stdout=output_file )
- 方法一:用
为什么双人查询能正常运行?
因为双人查询的JSON语法完全正确,且结构相对简单,没有触发shell转义的问题,所以API能正常解析。
内容的提问来源于stack exchange,提问作者Aaron Meese
相关产品推荐
相关产品推荐

