【问题标题】:How do I fix daily Limit Exceeded when using the .NET client for the google translation API?将 .NET 客户端用于 google 翻译 API 时,如何解决每日超出限制?
【发布时间】:2017-10-26 09:42:35
【问题描述】:

我正在尝试实现一个小型 slackbot,我想在其中利用 google 翻译 API,但我很难让它工作。我确实设法运行了一个请求,但在一个成功的请求之后,我不断收到“403 - 超出每日限制”消息。

我正在使用 F#,我的简单代码如下所示:

module Translation =
    open Google.Cloud.Translation.V2
    let client = TranslationClient.Create()

    let detectLanguage text =
        let detection = client.DetectLanguage(text)
        let res = sprintf "%A %A %A %A" detection.Confidence detection.IsReliable detection.Language detection.Text
        res

当我调用“detectLanguage”时,我得到了这个异常:

未处理的异常:Google.GoogleApiException:Google.Apis.Requests.RequestError 超出每日限额 [403] 错误 [ 消息[超出每日限制] 位置[-] 原因[dailyLimitExceeded] 域[usageLimits] ] 在 Google.Apis.Requests.ClientServiceRequest1.Execute() at Google.Cloud.Translation.V2.TranslationClientImpl.DetectLanguages(IEnumerable1 textItems) 在 Google.Cloud.Translation.V2.TranslationClient.DetectLanguage(字符串文本)

当我检查控制台时,我看不到任何对 API 的请求,所以我认为它可能没有正确配置。我该如何解决这个问题?

我首先在论坛https://groups.google.com/forum/#!topic/google-translate-api/5JHj3RLrH7o 中发布了问题,并得到了一条评论,我需要正确验证。据我了解,.NET 客户端 TranslationClient.Create() 应该使用默认凭据(如果我没有指定任何凭据),这适用于我使用的所有其他谷歌工具。

【问题讨论】:

    标签: .net google-cloud-platform google-translate


    【解决方案1】:

    是的,TranslationClient.Create 将使用默认凭据 - 但在这种情况下,几乎可以肯定它们是错误默认凭据,假设您在本地运行它。如果您在 Google Cloud Platform 上运行它,这应该不是问题 - 使用“环境”凭据应该没问题。 (如果没有,请在评论中告诉我,我们可以尝试进一步诊断。)

    如果您通过运行gcloud auth application-default login 或类似方法获得了默认应用程序凭据,那么虽然它会发送您的凭据,但它会尝试用完错误的配额。我不会详细说明原因,部分是因为我会弄错,部分是因为它是一个复杂的混乱。对此感到抱歉。

    好消息是它相当容易修复。基本上,you want to use service account credentials:

    • 如果您还没有服务帐号,请创建一个服务帐号
    • 下载带有凭据的 JSON 文件
    • 设置 GOOGLE_APPLICATION_CREDENTIALS 环境变量以指定 JSON 文件的位置

    默认应用程序凭据将加载该凭据文件并使用该文件进行身份验证。它将使用正确的项目进行配额,一切都应该很好。

    【讨论】:

    • 听起来可能是这样,我今天晚些时候会测试和验证。有没有办法使用“我的”凭据,就像我对所有其他类型的服务所做的那样,或者如果您想在本地运行它是否需要服务帐户?就像“强制”针对特定项目的身份验证?
    • @TomasJansson:嗯,您可能会获得真正的用户凭据,但我认为您不能使用 gcloud auth 凭据。不过,服务帐户是推荐的方法。
    猜你喜欢
    • 2014-03-20
    • 2021-10-04
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 2014-07-08
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多