#!coding:utf-8
import urllib
import json
import base64
import os
import time
def getToken():
Api_Key = 'hSCATgTQCvoE50cvNOkM37wm'
Secrect_Key = '969430e14e7a654a89bf32aa63654346'
url = 'https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + Api_Key + '&client_secret=' + Secrect_Key
res = urllib.urlopen(url).read()
data = json.loads(res)
token = data['access_token'] # 获取的token
print 'token获取成功:', token
return token
def getYuYin(token, text):
data = {'tex': text, 'lan': 'zh', 'cuid': '68-07-15-92-7C-2D', 'ctp': '1',
'spd': '4', 'vol': '8',
'tok': token}
date_urlencode = urllib.urlencode(data)
print date_urlencode
ServerUrl = 'http://tsn.baidu.com/text2audio'
r = urllib.urlopen(ServerUrl, str.encode(date_urlencode))
print r.getcode()
result = r.read()
file = open(r"voice.mp3", "wb")
file.write(result)
file.close()
token = getToken()
text = '你好我叫吴建豪,事件号IM12343发生故障,请及时处理,Jean,please finish it.'
getYuYin(token, text)