Python求助~调用百度API
本帖最后由 快乐的小驹 于 2024-1-21 10:29 编辑当前代码:
CLIENT_ID = "99999"# 你的apikey,需要修改
CLIENT_SECRET = "99999"# 你的Secret Key,需要修改
# 获取access_token
def get_access_token():
auth_url = 'https://aip.baidubce.com/oauth/2.0/token'
params = {
'grant_type': 'client_credentials',
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
}
response = requests.post(auth_url, data=params)
data = response.json()
access_token = data.get('access_token')
if not access_token:
raise "请输入正确的API_KEY 和 SECRET_KEY"
return access_token
修改后的:
#百度智能云账号,填写上自己的百度智能云开发帐号信息
with open('config.txt', 'r') as f:
lines = f.readlines()
APP_ID = lines.strip('APP_ID:')# strip()用于移除字符串首尾的空白字符
API_KEY = lines.strip('API_KEY:')
SECRET_KEY = lines.strip('SECRET_KEY:')
# 获取access_token
def get_access_token():
auth_url = 'https://aip.baidubce.com/oauth/2.0/token'
params = {
'grant_type': 'client_credentials',
'client_id': API_KEY ,
'client_secret': SECRET_KEY ,
}
response = requests.post(auth_url, data=params)
data = response.json()
access_token = data.get('access_token')
if not access_token:
raise "请输入正确的API_KEY 和 SECRET_KEY"
return access_token
修改后的症状:config.txt文件里的API_KEY和SECRET_KEY到不了字典里。
求大神给解决一下。 我的建议是暴力replace
APP_ID = lines.replace("APP_ID:", "") APP_ID = lines.strip('APP_ID:')# strip()用于移除字符串首尾的空白字符
API_KEY = lines.strip('API_KEY:')
SECRET_KEY = lines.strip('SECRET_KEY:') kof21411 发表于 2024-1-20 17:59
APP_ID = lines.strip('APP_ID:')# strip()用于移除字符串首尾的空白字符
API_KEY = lines.str ...
raise "请输入正确的API_KEY 和 SECRET_KEY"
TypeError: exceptions must derive from BaseException APP_ID = lines.strip().strip('APP_ID:')# strip()用于移除字符串首尾的空白字符
API_KEY = lines.strip().strip('API_KEY:')
SECRET_KEY = lines.strip().strip('SECRET_KEY:') ~零度 发表于 2024-1-20 18:19
APP_ID = lines.strip().strip('APP_ID:')# strip()用于移除字符串首尾的空白字符
API_KEY = lines
也不行~还是没有值~ 检测config.txt的编码格式,确定是UTF-8无BOM
with open('config.txt', 'r') as f:
lines = f.readlines()
APP_ID = lines.strip('APP_ID:').strip()# strip()用于移除字符串首尾的空白字符
API_KEY = lines.strip().strip('API_KEY:')
SECRET_KEY = lines.strip().strip('SECRET_KEY:')
print(APP_ID)
print(API_KEY)
print(SECRET_KEY)
config.txt
APP_ID:9999
API_KEY:9999
SECRET_KEY:99999 wangtietou 发表于 2024-1-20 18:38
检测config.txt的编码格式,确定是UTF-8无BOM
with open('config.txt', 'r') as f:
lines = f.readli ...
print(APP_ID)
print(API_KEY)
print(SECRET_KEY)
是正常的~ 既然是用python,就用百度AI的SDK好了
https://github.com/Baidu-AIP/python-sdk wangtietou 发表于 2024-1-20 18:47
既然是用python,就用百度AI的SDK好了
https://github.com/Baidu-AIP/python-sdk
代码都写完了~就卡这一步了~ """
获取token
"""
def fetch_token():
params = {'grant_type': 'client_credentials',
'client_id': API_KEY,
'client_secret': SECRET_KEY}
post_data = urlencode(params)
if (IS_PY3):
post_data = post_data.encode('utf-8')
req = Request(TOKEN_URL, post_data)
try:
f = urlopen(req, timeout=5)
result_str = f.read()
except URLError as err:
print('token http response http code : ' + str(err.code))
result_str = err.read()
if (IS_PY3):
result_str = result_str.decode()
result = json.loads(result_str)
if ('access_token' in result.keys() and 'scope' in result.keys()):
if not 'audio_tts_post' in result['scope'].split(' '):
print ('please ensure has check the tts ability')
exit()
return result['access_token']
else:
print ('please overwrite the correct API_KEY and SECRET_KEY')
exit()
"""TOKEN end """
页:
[1]
2