简体   繁体   English

jQuery Google 翻译 403 翻译调用错误

[英]jQuery Google Translate 403 Error on translation call

I'm trying to make a call to google translate, I use my google API key and I keep getting a 403 error cant seem to figure out.我正在尝试拨打谷歌翻译,我使用我的谷歌 API 密钥,但我不断收到 403 错误,似乎无法弄清楚。 I think I'm using the proper script type HTML tag "application/javascript" cant seem to figure this out我想我正在使用正确的脚本类型 HTML 标签“应用程序/javascript”似乎无法解决这个问题

This is the error in the chrome console:这是 chrome 控制台中的错误:

GET https://www.googleapis.com/language/translate/v2?key=API KEY REMOVED&source=en&target=de&q=How%20are%20you 403

   send @ jquery.min.js:2
   ajax @ jquery.min.js:2
   S.<computed> @ jquery.min.js:2
   translatePage @ (index):26
   onclick @ (index):17

using this code:使用此代码:

<html>
  <body>
  <head>
    <script type="application/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  </head>
   
   <b>Text:</b> <input type="text" id="text" value="How are you"/> <br>
    <b>Target Langugage:</b> 
    <select id="target">
    <option value="de">Germany</option>
    <option value="hi">Hindi</option>
    <option value="fr">French</option>
    </select>
    
    <br>
    
    <input type="button" value="Translate"  onclick="translatePage()" />
    
    <br> <br>
    
    <b>Translated Text:</b><div id="translated"></div>   
    
    <script>
    function translatePage()
    {
        $.get("https://www.googleapis.com/language/translate/v2",
            {
            key:"GOOGLE API KEY",
            source:"en",
            target:$("#target").val(),
            q:$("#text").val()
            },
            function(response)
            {
                $("#translated").html(response.data.translations[0].translatedText);
 
            },"json") .fail(function(jqXHR, textStatus, errorThrown) 
            {
                alert( "error :"+errorThrown );
            });
    }
    </script>
  
  </body>
</html>

403 error means that you have permission issues. 403 错误意味着您有权限问题。 I tried running your code and it works fine for me, I used an API key of mine with no restrictions.我尝试运行您的代码,它对我来说很好,我使用了我的 API 密钥,没有任何限制。

Test done without API key restrictions:在没有 API 密钥限制的情况下完成的测试:

在此处输入图像描述

I tried adding restrictions on my API key like only "allowing requests for Cloud Storage" as an example and I get your 403 error.我尝试在我的 API 密钥上添加限制,例如仅“允许请求云存储”,但我收到了 403 错误。

Test done using an API key with API restrictions.使用具有 API 限制的 API 密钥完成测试。 As you can see "Hindi" translation did not push through due to the applied restriction:如您所见,由于应用的限制,“印地语”翻译没有通过:

在此处输入图像描述

I suggest to double check your API key setup if there are existing Application or API restrictions and adjust them accordingly.如果存在现有的应用程序或 API 限制,我建议仔细检查您的 API 密钥设置并相应地调整它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM