You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET使用google.cloud.translation.v2如何指定API密钥?

解决方案

你当前使用的无参TranslationClient.Create()方法默认会读取环境中的服务账号凭证,所以会触发身份验证报错。Google.Cloud.Translation.V2库提供了专门的CreateFromApiKey方法用于传入API密钥初始化客户端,直接替换原有客户端创建逻辑即可:

Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click
    ' 替换为你从Google Cloud控制台获取的Translation V2 API密钥
    Dim apiKey As String = "你的实际API密钥"
    Dim client As TranslationClient = TranslationClient.CreateFromApiKey(apiKey)
    Dim result As TranslationResult = client.TranslateText(TextBox4.Text, LanguageCodes.English, LanguageCodes.German, TranslationModel.Base)
    TextBox5.Text = result.TranslatedText
End Sub

注意事项

  • 调用前请确认你已经在Google Cloud控制台启用了Cloud Translation API,且生成的API密钥没有设置与当前运行环境不匹配的限制(如IP限制、应用标识限制等),否则仍会触发授权错误。
  • 生产环境不要将API密钥硬编码在业务代码中,建议存储在加密的配置文件、密钥管理服务等安全位置,避免密钥泄露导致资产损失。
  • 如果调用仍报错,可以先检查API配额是否超限、API密钥是否有权限访问Translation V2接口。

内容的提问来源于stack exchange,提问作者sharkyenergy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 17:45:02