我们今天使用Python发送网络请求到百度人脸识别API接口。
我们需要requests模块,它需要安装,代码是
pip3 install requests
注意:不好意思各位,我的配额没了!如需使用请在getToken
里边更换为自己的key
!
首先我们先来介绍一下API是什么:
API就是操作系统留给应用程序的一个调用接口,应用程序通过调用操作系统的 API 而使操作系统去执行应用程序的命令。
介绍完了,我们开始写代码:
#导入所需模块
from tkinter import Tk
from tkinter.messagebox import showinfo,showerror
from tkinter.filedialog import askopenfilename
from requests import post,get
from base64 import b64encode
#声明变量
totalList = []
#获取百度人脸识别接口
def getToken():
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials' \
'&client_id=T1KUrVlob1vUuLpQ0sOYrfoB&client_secret=obx6I60FomQIHqRwlx3mp1GXTGKOkHPu'
response = get(host)
content = response.json()
content = content[