1、點擊技術文檔
2、將代碼拷貝到pycharm獲取access_token
# encoding:utf-8
import requests
# client_id 為官網獲取的AK, client_secret 為官網獲取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官網獲取的AK】&client_secret=【官網獲取的SK】'
response = requests.get(host)
if response:
print(response.json())
3、
4、將該功能封裝成一個函數
# 獲取access_token函數
def get_access_token(client_id="3bbdU8i9o7bMg4Wg0KSbNamr", client_secret="GvesuaedzQucRZdvgf9FikS57yc00xMd"):
# client_id 為官網獲取的AK, client_secret 為官網獲取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=3bbdU8i9o7bMg4Wg0KSbNamr&client_secret=GvesuaedzQucRZdvgf9FikS57yc00xMd'
print(host)
response = requests.get(host)
if response:
print(response.json(), type(response.json()))
# 訪問服務器所給的令牌口令
print(response.json()["access_token"])
if __name__ == "__main__":
get_access_token()