鱼C论坛

 找回密码
 立即注册
查看: 1582|回复: 2

对接百度语音合成API出现请求连接不合法问题

[复制链接]
发表于 2022-6-29 22:37:47 | 显示全部楼层 |阅读模式
10鱼币
使用百度云的在线语音合成时,出现以下错误:

  1. tts api  error:{"error_code":100,"error_msg":"Invalid parameter"}
  2. file saved as : error.txt
  3. Press any key to continue . . .
复制代码


代码源码:

  1. # -*- coding: UTF-8 -*-

  2. import sys
  3. import json

  4. # 保证兼容python2以及python3
  5. IS_PY3 = sys.version_info.major == 3
  6. if IS_PY3:
  7.     from urllib.request import urlopen
  8.     from urllib.request import Request
  9.     from urllib.error import URLError
  10.     from urllib.parse import urlencode
  11.     from urllib.parse import quote_plus
  12. else:
  13.     import urllib2
  14.     from urllib import quote_plus
  15.     from urllib2 import urlopen
  16.     from urllib2 import Request
  17.     from urllib2 import URLError
  18.     from urllib import urlencode

  19. # 替换你的 API_KEY
  20. API_KEY = 'xxxxxxxxxxxxxxxxx'

  21. # 替换你的 SECRET_KEY
  22. SECRET_KEY = 'xxxxxxxxxxxxxxxxxxx'

  23. # 大姚的订单信息内容文本
  24. TEXT = ''内容已省去'''



  25. TTS_URL = 'https://aip.baidubce.com/rpc/2.0/tts/v1/create'

  26. """  TOKEN start """

  27. TOKEN_URL = 'http://openapi.baidu.com/oauth/2.0/token'


  28. """
  29.     获取token
  30. """
  31. def fetch_token():
  32.     params = {'grant_type': 'client_credentials',
  33.               'client_id': API_KEY,
  34.               'client_secret': SECRET_KEY}
  35.     post_data = urlencode(params)
  36.     if (IS_PY3):
  37.         post_data = post_data.encode('utf-8')
  38.     req = Request(TOKEN_URL, post_data)
  39.     try:
  40.         f = urlopen(req, timeout=5)
  41.         result_str = f.read()
  42.     except URLError as err:
  43.         print('token http response http code : ' + str(err.code))
  44.         result_str = err.read()
  45.     if (IS_PY3):
  46.         result_str = result_str.decode()


  47.     result = json.loads(result_str)

  48.     if ('access_token' in result.keys() and 'scope' in result.keys()):
  49.         if not 'audio_tts_post' in result['scope'].split(' '):
  50.             print ('please ensure has check the tts ability')
  51.             exit()
  52.         return result['access_token']
  53.     else:
  54.         print ('please overwrite the correct API_KEY and SECRET_KEY')
  55.         exit()


  56. """  TOKEN end """

  57. if __name__ == '__main__':

  58.     token = fetch_token()

  59.     tex = quote_plus(TEXT)  # 此处TEXT需要两次urlencode

  60.     params = {
  61.              'access_token':token,
  62.              'text': tex,
  63.              'format':"mp3-48k",
  64.              'voice':5003,
  65.               'lang': "zh"
  66.               }

  67.     data = urlencode(params)

  68.     req = Request(TTS_URL, data.encode('utf-8'))
  69.     has_error = False
  70.     try:
  71.         f = urlopen(req)
  72.         result_str = f.read()

  73.         headers = dict((name.lower(), value) for name, value in f.headers.items())

  74.         has_error = ('content-type' not in headers.keys() or headers['content-type'].find('audio/') < 0)
  75.     except  URLError as err:
  76.         print('http response http code : ' + str(err.code))
  77.         result_str = err.read()
  78.         has_error = True

  79.     save_file = "error.txt" if has_error else u'大姚的订单信息.mp3'

  80.     with open(save_file, 'wb') as of:
  81.         of.write(result_str)

  82.     if has_error:
  83.         if (IS_PY3):
  84.             result_str = str(result_str, 'utf-8')
  85.         print("tts api  error:" + result_str)

  86.     print("file saved as : " + save_file)
复制代码


应该是请求的链接不合法,有没有大神知道如何写合法链接?万分感谢!

这是相关文档:

https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu
https://cloud.baidu.com/doc/SPEECH/s/1ku59x8ey

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-6-30 01:47:20 From FishC Mobile | 显示全部楼层
返回值不是写着无效参数吗?为什么会是链接不合法?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-6-30 10:15:54 | 显示全部楼层
hrpzcf 发表于 2022-6-30 01:47
返回值不是写着无效参数吗?为什么会是链接不合法?

我不懂,参数无效的话,浏览器请求的链接也就不合法了吧?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 09:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表