import requests import os import base64 import json os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="./api.json" url = "https://texttospeech.googleapis.com/v1/text:synthesize" headers = {'Content-Type': 'application/json; charset=utf-8', 'X-Goog-Api-Key': 'synclub-2383kjhjksxfv.2341gs' # 待补充 } text = "二月の下旬に差し掛かる頃だった。" data = { "input":{ "text":text }, "voice":{ "languageCode":"ja-JP", "name":"ja-JP-Neural2-C", "ssmlGender":"MALE" }, "audioConfig":{ "audioEncoding":"MP3" } } response = requests.post(url, headers=headers, json=data) response = response.json() print(response) audio = response['audioContent'] audio = base64.b64decode(audio) # The response's audio_content is binary. with open("test.mp3", "wb") as out: out.write(audio)