如何在Java桌面应用中完成Google文本转语音的身份认证?
Google Text-to-Speech Java 非Web应用认证指南
问题1:Google Cloud端需做哪些配置?需要API Key还是服务账号?
- 首先必须启用Text-to-Speech API:在Google Cloud控制台的API库中找到Text-to-Speech API,点击启用。
- 认证优先用服务账号(Service Account),这是非Web/服务器端应用的标准认证方案;API Key更适合简单前端请求,不推荐用于后端Java应用——它易泄露且权限控制能力弱。
- 给服务账号分配权限:至少需要
roles/cloudtexttospeech.speechSynthesizer角色,确保它能调用语音合成接口。
问题2:如何生成服务账号JSON凭证文件?
不用依赖gcloud工具,直接在控制台操作即可:
- 进入Google Cloud控制台的「IAM与管理」->「服务账号」页面
- 找到你创建的服务账号,点击账号名称进入详情页
- 切换到「密钥」标签页
- 点击「添加密钥」->「创建新密钥」
- 密钥类型选「JSON」,点击「创建」,浏览器会自动下载JSON文件,将其放到Java项目可访问的路径(比如项目根目录)
如果一定要用Windows的gcloud命令行(需先安装gcloud SDK),执行:
gcloud iam service-accounts keys create tts.json --iam-account 你的服务账号邮箱@项目ID.iam.gserviceaccount.com
问题3:已有API Key和项目ID,能否不用JSON文件直接认证?
官方Java客户端库不支持直接用API Key初始化TextToSpeechClient,因为SDK是基于OAuth2服务账号凭证设计的。如果想绕开JSON文件,有两种替代方案:
- 用环境变量传递凭证:设置
GOOGLE_APPLICATION_CREDENTIALS环境变量为JSON文件的绝对路径,代码里直接调用TextToSpeechClient.create()即可——SDK会自动读取环境变量加载凭证。 - 若非要用API Key,只能放弃官方SDK,直接调用Text-to-Speech的REST API,在请求URL中带上
key=你的API Key参数,但这种方式需要自己处理HTTP请求、数据序列化/反序列化,不如SDK便捷。
格式化后的可运行代码
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import com.google.api.gax.core.CredentialsProvider; import com.google.api.gax.core.FixedCredentialsProvider; import com.google.auth.oauth2.ServiceAccountCredentials; import com.google.cloud.texttospeech.v1.AudioConfig; import com.google.cloud.texttospeech.v1.AudioEncoding; import com.google.cloud.texttospeech.v1.SsmlVoiceGender; import com.google.cloud.texttospeech.v1.SynthesisInput; import com.google.cloud.texttospeech.v1.SynthesizeSpeechResponse; import com.google.cloud.texttospeech.v1.TextToSpeechClient; import com.google.cloud.texttospeech.v1.TextToSpeechSettings; import com.google.cloud.texttospeech.v1.VoiceSelectionParams; import com.google.protobuf.ByteString; public class GoogleTextToSpeech { protected TextToSpeechClient textToSpeechClient; VoiceSelectionParams voice; AudioConfig audioConfig; public GoogleTextToSpeech() { String jsonPath = "tts.json"; try { CredentialsProvider credentialsProvider = FixedCredentialsProvider.create( ServiceAccountCredentials.fromStream(new FileInputStream(jsonPath)) ); TextToSpeechSettings settings = TextToSpeechSettings.newBuilder() .setCredentialsProvider(credentialsProvider) .build(); textToSpeechClient = TextToSpeechClient.create(settings); voice = VoiceSelectionParams.newBuilder() .setLanguageCode("en-US") .setSsmlGender(SsmlVoiceGender.FEMALE) .build(); audioConfig = AudioConfig.newBuilder() .setAudioEncoding(AudioEncoding.LINEAR16) .build(); } catch (IOException e1) { System.err.println(e1.getMessage()); e1.printStackTrace(); } } public void speak(String message) { SynthesisInput input = SynthesisInput.newBuilder().setText(message).build(); SynthesizeSpeechResponse response = textToSpeechClient.synthesizeSpeech(input, voice, audioConfig); ByteString audioContents = response.getAudioContent(); try (OutputStream out = new FileOutputStream("output.wav")) { out.write(audioContents.toByteArray()); System.out.println("Audio content written to file \"output.wav\""); } catch (IOException e) { e.printStackTrace(); } } }
内容的提问来源于stack exchange,提问作者Alex
相关产品推荐
相关产品推荐

