(总)百度AI 平台API调用

近来要了解自然语言处理方面的技术,拿百度API做个实验对,进行多次编码尝试最终成功调用。

在本人博客百度API使用系列,使用python代码实现。涉及内容如下:

  1. AccessToken获取
  2. 自然语言API调用,代码参数设置
  3. 代码修改中出现的错误,及最终的方法  
  • 错误提示:"error_code": 282004,error_msg":"invalid parameter(s)"
  •     not a valid non-string sequence or mapping object

本节是AccessToken的获取,编写原因是因为百度官方说明是针对python2的代码,使用urllib2在python已经弃用,且给出的获取代码还是需要加工才能保证整个调用流程的连贯性。现把使用过程进行分享。

欢迎分享和转载,请注明出处 shuihupo:https://blog.csdn.net/shuihupo/article/details/79862004点击打开链接

同时本博客文章也会在 云南省高校数据化运营管理工程研究中心 博客发表

实验使用的环境是Windows10  Python3

首相查看官方说明文档

获取Access Token

请求URL数据格式

向授权服务地址https://aip.baidubce.com/oauth/2.0/token发送请求(推荐使用POST),并在URL中带上以下参数:

  • grant_type: 必须参数,固定为client_credentials
  • client_id: 必须参数,应用的API Key
  • client_secret: 必须参数,应用的Secret Key

例如:

https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=Va5yQRHlA4Fq5eR3LT0vuXV4&client_secret=0rDSjzQ20XUj5itV6WRtznPQSzr5pVw2&

形象化即:

https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的AK】&client_secret=【官网获取的SK】

参数是个人创建应用的参数或者自己应用的匹配密钥。创建应用参见博文点击打开链接


获取AccessToken代码:

代码的优势不仅在于得到access_token,更在于将其存为变量,方便随后的直接使用。

#!/ Mypython
# -*- coding: utf-8 -*-
# @Time    : 2018/4/4 18:36
# @Author  : LinYimeng
# @File    : test1.py
# @Software: PyCharm
import urllib
###第一步:获取access_token
# client_id 为官网获取的AK, client_secret 为官网获取的SK,以下一行按自己实际填写
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=****grant_type&client_id=****&client_secret=****'
request = urllib.request.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib.request.urlopen(request)
content = response.read()
if (content):
    print(type(content))#<class 'bytes'>
content_str=str(content, encoding="utf-8")
###eval将字符串转换成字典
content_dir = eval(content_str)
access_token = content_dir['access_token']

若请求错误,服务器将返回的JSON文本包含以下参数:

  • error: 错误码;关于错误码的详细信息请参考下方鉴权认证错误码。
  • error_description: 错误描述信息,帮助理解和解决发生的错误。

鉴权认证错误码

error error_description 解释
invalid_client unknown client id API Key不正确
invalid_client Client authentication failed Secret Key不正确
欢迎分享和转载,请注明出处 shuihupo:https://blog.csdn.net/shuihupo/article/details/79862004点击打开链接

实验使用的环境是Windows10  Python3

(三)百度AI 开放平台API调用之应用实践

选择调用的接口,eg:

欢迎分享和转载,请注明出处 shuihupo:https://blog.csdn.net/shuihupo/article/details/79862004
实验使用的环境是Windows10  Python3

词法分析接口

仔细阅读官方接口说明

  • 接口描述
  • 请求说明
    • HTTP方法: POST
    • (通用版)请求URL: https://aip.baidubce.com/rpc/2.0/nlp/v1/lexer
    • (定制版)请求URL: https://aip.baidubce.com/rpc/2.0/nlp/v1/lexer_custom
    • URL参数:
    参数
    access_token 通过API Key和Secret Key获取的access_token,参考“Access Token获取
    • Header如下:
    参数
    Content-Type application/json
    • body请求示例:

      {
        "text": "百度是一家高科技公司"
      }
      

    请求参数

    参数名称 类型 详细说明
    text string 待分析文本(目前仅支持GBK编码),长度不超过20000字节

POST方式调用

注意:要求使用JSON格式的结构体来描述一个请求的具体内容。**发送时默认需要对body整体进行GBK编码。**若使用UTF-8编码,请在url参数中添加charset=UTF-8 (大小写敏感) 例如:https://aip.baidubce.com/rpc/2.0/nlp/v1/lexer?charset=UTF-8&access_token=24.f9ba9c5241b67688bb4adbed8bc91dec.2592000.1485570332.282335-8574074

返回格式

JSON格式,返回内容为GBK编码

这些注意事项提醒我们要注意编码的问题。

法一:使用使用urllib时会返回如下错误,搜索解决方法是因为传入数据是字典需要编码。
post_data=json.dumps(post_data).encode('GBK')

282004 invalid parameter(s) 请求中包含非法参数,请检查后重新尝试

也不说怎么错了,上正确的代码吧。

 
 
#!/ Mypython
# -*- coding: utf-8 -*-
# @Time    : 2018/3/28 16:48
# @Author  : LinYimeng
# @File    : baidulearn.py
# @Software: PyCharm


import urllib
import urllib3
import json


 ###第一步:获取access_token
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=pd152AVsnYc6Nbfk5Yuh9XTh&client_secret=ZTn7ASCKAGgPFEFmMxszEoQmAlVb2LRM'
request = urllib.request.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib.request.urlopen(request)
content = response.read()
if (content):
    print(type(content))#<class 'bytes'>
content_str=str(content, encoding="utf-8")
###
content_dir = eval(content_str)
access_token = content_dir['access_token']
####第二部分调用
###api地址
import sys
print(sys.getdefaultencoding())
http=urllib3.PoolManager()
url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/lexer?access_token="+access_token
data = {"text":"昆明会下雨吗?"}
encodedata=json.dumps(data).encode('GBK')
request = urllib.request.Request(url, encode_data)
request.add_header('Content-Type', 'application/json')
response = urllib.request.urlopen(request)
content = response.read()  # content是一个utf-8格式的<class 'bytes'>
#bytes ⇒ str:str(b, encoding='utf-8')
content_str = str(content, encoding="gbk")
#  content被编码为gbk格式的字节串,赋给content_str
print(type(content_str))
if (content_str):
    print(content_str)

法二:使用使用urllib3

#!/ Mypython
# -*- coding: utf-8 -*-
# @Time    : 2018/3/28 16:48
# @Author  : LinYimeng
# @File    : baidulearn.py
# @Software: PyCharm

import urllib
import urllib3
import json

 ###第一步:获取access_token
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=pd152AVsnYc6Nbfk5Yuh9XTh&client_secret=ZTn7ASCKAGgPFEFmMxszEoQmAlVb2LRM'
request = urllib.request.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib.request.urlopen(request)
content = response.read()
if (content):
    print(type(content))#<class 'bytes'>
content_str=str(content, encoding="utf-8")
###
content_dir = eval(content_str)
access_token = content_dir['access_token']
####第二部分调用
###api地址
import sys
print(sys.getdefaultencoding())
http=urllib3.PoolManager()
url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/lexer?access_token="+access_token
print(url)
data ={"text":"昆明会下雨吗"}
encode_data= json.dumps(data).encode('GBK')
#JSON:在发起请求时,可以通过定义body 参数并定义headers的Content-Type参数来发送一个已经过编译的JSON数据:
request = http.request('POST',
                       url,
                       body=encode_data,
                       headers={'Content-Type':'application/json'}
                       )
result = str(request.data,'GBK')
print(result)

结果:{"log_id": 7265183527788840706, "text": "今天北京天气怎么样?", "items": [{"loc_details": [], "byte_offset": 0, "uri": "", "pos": "", "ne": "TIME", "item": "今天", "basic_words": ["今天"], "byte_length": 4, "formal": ""}, {"loc_details": [], "byte_offset": 4, "uri": "", "pos": "", "ne": "LOC", "item": "北京", "basic_words": ["北京"], "byte_length": 4, "formal": ""}, {"loc_details": [], "byte_offset": 8, "uri": "", "pos": "n", "ne": "", "item": "天气", "basic_words": ["天气"], "byte_length": 4, "formal": ""}, {"loc_details": [], "byte_offset": 12, "uri": "", "pos": "r", "ne": "", "item": "怎么样", "basic_words": ["怎么", "样"], "byte_length": 6, "formal": ""}, {"loc_details": [], "byte_offset": 18, "uri": "", "pos": "w", "ne": "", "item": "?", "basic_words": ["?"], "byte_length": 2, "formal": ""}]}

可以是识别到“昆明”是地名等信息。








猜你喜欢

转载自blog.csdn.net/shuihupo/article/details/79862004