汉语检索282004错误
冥静的哀怨 发布于2021-04 浏览:29078 回复:2
0
收藏
# -*- coding: utf-8 -*-
import json

import requests


def get_token():
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=AgT29SeGco67Yw4gYIbXRqFc&client_secret=eSZhk1Fg6G56XP17vEcu5G8stYEWZhNG'
    response = requests.get(host)
    return response.json()["access_token"]


def connect():
    BASE_URL = 'https://aip.baidubce.com/rpc/2.0/kg/v1/cognitive/chinese_search'
    access_token = get_token()
    url = BASE_URL + '?access_token=' + access_token
    headers = {'Content-Type': 'application/json'}
    data = {"query": "愚公移山"}
    response = requests.post(url, data=data, headers=headers)
    result = json.loads(response.content)
    print(result)


if __name__ == '__main__':
    connect()

输出结果是{'log_id': 4136634338084344929, 'error_code': 282004, 'error_msg': 'invalid parameter(s)'}

请问一下这是什么问题呢?

收藏
点赞
0
个赞
共2条回复 最后由用户已被禁言回复于2022-03
#3用户已被禁言回复于2021-04

这个错误是 参数错误。和企业认证没关系。

是你对HTTP不够了解。接口要求JSON格式。

反正Python到底是怎么规定的我也不太清楚。我找相关Python代码测试了一下。接口是没有问题的。

import urllib3
import json

access_token=''
http=urllib3.PoolManager()
url='https://aip.baidubce.com/rpc/2.0/kg/v1/cognitive/chinese_search?access_token='+access_token

params = {"query": "愚公移山"}
# 对参数params数据进行json处理
json_data = json.dumps(params).encode('utf-8')
request=http.request('POST',
                     url,
                     body=json_data,
                     headers={'Content-Type':'application/json'})
result = str(request.data,'utf-8')
print('结果'+result)

返回内容

{
    "log_id":3182384419190199009,
    "result":[
        {
            "request":{
                "query":"愚公移山",
                "queryType":"entity"
            },
            "response":{
                "sourceUrl":"https://hanyu.baidu.com/zici/s?wd=%E6%84%9A%E5%85%AC%E7%A7%BB%E5%B1%B1&query=%E6%84%9A%E5%85%AC%E7%A7%BB%E5%B1%B1&srcid=28243&from=kg2",
                "title":"愚公移山",
                "sourceName":"百度汉语",
                "entity":[
                    {
                        "attrs":[
                            {
                                "objects":[
                                    {
                                        "@value":"https://hanyu.baidu.com/zici/s?wd=%E6%84%9A%E5%85%AC%E7%A7%BB%E5%B1%B1&query=%E6%84%9A%E5%85%AC%E7%A7%BB%E5%B1%B1&srcid=28243&from=kg2"
                                    }
                                ],
                                "type":"simple",
                                "key":"url",
                                "label":"详情页"
                            },
                            {
                                "objects":[
                                    {
                                        "@value":"虎头蛇尾"
                                    },
                                    {
                                        "@value":"有头无尾"
                                    }
                                ],
                                "type":"simple",
                                "key":"antonym",
                                "label":"反义词"
                            },
                            {
                                "objects":[
                                    {
                                        "@value":"传说古代有一位老人名叫北山愚公,家门前有两座大山挡住了路,他下决心要把山平掉,另一个老人河曲智叟笑他太傻,认为不可能。愚公回答说:“我死了有儿子,儿子死了还有孙子,子子孙孙是没有穷尽的。这两座山可不会再增高了,凿去一点就少一点,终有一天要凿平的。”(见于《列子·汤问》)比喻做事有毅力,有恒心,不怕困难。"
                                    }
                                ],
                                "type":"simple",
                                "key":"definition",
                                "label":"释义"
                            },
                            {
                                "objects":[
                                    {
                                        "@value":"https://dss0.baidu.com/6KAZsjip0QIZ8tyhnq/text2audio?tex=%E6%84%9A%28yu2%29%E5%85%AC%28gong1%29%E7%A7%BB%28yi2%29%E5%B1%B1%28shan1%29&cuid=dict&lan=ZH&ctp=1&pdt=30&vol=9&per=4100"
                                    }
                                ],
                                "type":"simple",
                                "key":"pronunciation",
                                "label":"读音"
                            },
                            {
                                "objects":[
                                    {
                                        "@value":"清·黄宗羲《张苍水墓志铭》:“愚公移山;精卫填海;常人藐为说铃;贤圣指为血路也。”"
                                    }
                                ],
                                "type":"simple",
                                "key":"source",
                                "label":"出处"
                            },
                            {
                                "objects":[
                                    {
                                        "@value":"[yú gōng yí shān]"
                                    }
                                ],
                                "type":"simple",
                                "key":"spell",
                                "label":"拼音"
                            },
                            {
                                "objects":[
                                    {
                                        "@value":"持之以恒"
                                    },
                                    {
                                        "@value":"铁杵磨针"
                                    },
                                    {
                                        "@value":"精卫填海"
                                    },
                                    {
                                        "@value":"铁杵成针"
                                    },
                                    {
                                        "@value":"锲而不舍"
                                    }
                                ],
                                "type":"simple",
                                "key":"synonym",
                                "label":"近义词"
                            },
                            {
                                "objects":[
                                    {
                                        "@value":"JWTM"
                                    }
                                ],
                                "type":"simple",
                                "key":"wubi",
                                "label":"五笔"
                            }
                        ],
                        "name":"愚公移山",
                        "@type":[
                            "idiom"
                        ]
                    }
                ],
                "answer":[
                    "愚公移山"
                ],
                "voice":"愚公移山,意思是:传说古代有一位老人名叫北山愚公,家门前有两座大山挡住了路,他下决心要把山平掉,另一个老人河曲智叟笑他太傻,认为不可能。愚公回答说:“我死了有儿子,儿子死了还有孙子,子子孙孙是没有穷尽的。这两座山可不会再增高了,凿去一点就少一点,终有一天要凿平的。”(见于《列子·汤问》)比喻做事有毅力,有恒心,不怕困难。"
            }
        }
    ]
}
1
#2冥静的哀怨回复于2021-04

我没有企业认证,但是有送的200个额度,是没有企业认证就不能使用这个功能吗

0
快速回复
TOP
切换版块