自己写短文本相似度的post,为什么报错
zhouwensmile 发布于2017-11 浏览:5384 回复:6
0
收藏

请问我根据官方文档写一个简单的post方法,为什么总是报错呢?是我对文档理解有问题吗?

 

# -*- coding: utf-8 -*-
__author__ = 'duohappy'


import requests
import json

url = 'https://aip.baidubce.com/rpc/2.0/nlp/v2/simnet'

API_Key = 'my_api_key'
Secret_Key = 'my_secret_key'

headers = {'Content-Type': 'application/json'}
payload = {'grant_type': 'client_credentials', 'client_id': API_Key, 'client_secret': Secret_Key}

post_text = json.dumps({"text1": '浙富股份', "text2": '万事通自考网'}, ensure_ascii=False).encode('gbk')

r = requests.post(url, params=payload, data=post_text, headers=headers)
print(r.content)
收藏
点赞
0
个赞
共6条回复 最后由周俊316回复于2017-12
#7周俊316回复于2017-12

解决了就好

0
#6zhouwensmile回复于2017-11
#5 用户已被禁言回复
https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=你的apikey&client_secret=你的secretkey 获取得到access_token值备用。里面的apikey secretkey自己替换。在控制台查找 https://aip.baidubce.com/rpc/2.0/nlp/v2/simnet?access_token=填写第一步获取得到的access_token参数的值 python调用API没做过。用SDK还是没问题的。我是Java。对python直接发送HTTP不会。我是示意一下意思: [代码]
展开

谢谢啦,确实是我的问题

重新发一下可用的代码

# -*- coding: utf-8 -*-
__author__ = 'duohappy'


import requests
import json

API_Key = 'your key'
Secret_Key = 'your key'


def get_token():
    token_url = 'https://aip.baidubce.com/oauth/2.0/token'

    token_headers = {'Content-Type':'application/json; charset=UTF-8'}
    
    payload = {'grant_type': 'client_credentials', 'client_id': API_Key, 'client_secret': Secret_Key}

    web_data = requests.post(token_url, headers=token_headers, params=payload)
    
    with open('token.json', 'w') as f:
        f.write(web_data.text)
    
    print(web_data.url)

def get_sim(access_token):
    
    post_url = 'https://aip.baidubce.com/rpc/2.0/nlp/v2/simnet'

    payload = {'access_token': access_token}
    headers = {'Content-Type': 'application/json'}

    post_text = json.dumps({"text_1": '浙富股份', "text_2": '万事通自考网'}, ensure_ascii=False).encode('gbk')
    
    web_data = requests.post(post_url, params=payload, data=post_text, headers=headers)
    
    print(web_data.text)
    print(web_data.url)
    
    
if __name__ == '__main__':
    get_token()
    
    with open('token.json', 'r') as f:
        token = json.load(f)
        
    get_sim(token['access_token'])
1
#5用户已被禁言回复于2017-11
#3 zhouwensmile回复
对,是python3 报错:b'{"error_msg":"Invalid parameter","error_code":100}' 最后请求的url:https://aip.baidubce.com/rpc/2.0/nlp/v2/simnet?grant_type=client_credentials&client_id=my_api_key&client_secret=my_secret_key url拼接的方法不是这样么?文档是这样写的
展开
  1. https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=你的apikey&client_secret=你的secretkey 获取得到access_token值备用。里面的apikey secretkey自己替换。在控制台查找
  2. https://aip.baidubce.com/rpc/2.0/nlp/v2/simnet?access_token=填写第一步获取得到的access_token参数的值
  3. python调用API没做过。用SDK还是没问题的。我是Java。对python直接发送HTTP不会。我是示意一下意思:
# -*- coding: utf-8 -*-
__author__ = 'duohappy'


import requests
import json
access_token=根据第一步的接口获取的access_token的值
//拼接在url后面
url = 'https://aip.baidubce.com/rpc/2.0/nlp/v2/simnet?access_token='+access_token

headers = {'Content-Type': 'application/json'}

post_text = json.dumps({"text1": '浙富股份', "text2": '万事通自考网'}, ensure_ascii=False).encode('gbk')

r = requests.post(url, params='', data=post_text, headers=headers)
print(r.content)
1
#4用户已被禁言回复于2017-11
#3 zhouwensmile回复
对,是python3 报错:b'{"error_msg":"Invalid parameter","error_code":100}' 最后请求的url:https://aip.baidubce.com/rpc/2.0/nlp/v2/simnet?grant_type=client_credentials&client_id=my_api_key&client_secret=my_secret_key url拼接的方法不是这样么?文档是这样写的
展开

 

 

你放的参数那是请求token用到的。接口地址也不是那个哦。你仔仔细细认认真真看下文档。从头看起。真心的就不会出现错了。

https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=你的apikey&client_secret=你的secretkey

你要把token放在simnet接口后面 蓝色地址才是去得到token。红色地址是请求接口的地址并拼接token。获取token地址和短文本相似度接口地址不一样的。红色接口地址只要拼接得到的token。不需要grant_type=client_credentials这些参数 。这文档都是有示例的。建议还是仔细看文档

https://aip.baidubce.com/rpc/2.0/nlp/v2/simnet?access_token=token

这个接口才是去获取具体接口返回的内容。

官网文档有说明  100 是无效的access_token参数,请检查后重新尝试

 

 

http://ai.baidu.com/docs#/Begin/top 接入指南文档

http://ai.baidu.com/docs#/NLP-API/top NLP各个接口使用文档

 

2
#3zhouwensmile回复于2017-11
#2 用户已被禁言回复
这是python? 具体报什么错误呢? 你的post是怎么写的。 那个payload获取到的accesstoken 你是拼接在url后面了吗?https://aip.baidubce.com/rpc/2.0/nlp/v2/simnet?access_token=payload
展开

对,是python3

报错:b'{"error_msg":"Invalid parameter","error_code":100}'

最后请求的url:https://aip.baidubce.com/rpc/2.0/nlp/v2/simnet?grant_type=client_credentials&client_id=my_api_key&client_secret=my_secret_key

url拼接的方法不是这样么?文档是这样写的

0
#2用户已被禁言回复于2017-11

这是python?

具体报什么错误呢?

你的post是怎么写的。

那个payload获取到的accesstoken 你是拼接在url后面了吗?https://aip.baidubce.com/rpc/2.0/nlp/v2/simnet?access_token=payload

1
TOP
切换版块