繁体   English   中英

Google翻译API返回403(PHP)

[英]Google Translate API returns 403 (PHP)

问题是关于Google Translate API

  1. 我设置了一个项目,然后打开了帐单(帐户上有一些钱)

  2. 我为服务器应用程序创建了一个密钥(也尝试使用浏览器密钥),并添加了我拥有的所有3个IP(家庭和2个服务器)

我所看到的:

  1. 它确实可以在apis-explorer和浏览器地址栏中工作( https://www.googleapis.com/language/translate/v2/detect?q=an%20english%20text&key=MY_KEY

  2. 如果我尝试从PHP代码获取相同的URL,它将返回403错误:

    $ apiKey ='MY_KEY';

    $ url =' https://www.googleapis.com/language/translate/v2/detect? q= an %20english%20text & key='。 $ apiKey。 '';

    $ handle = curl_init($ url);

    curl_setopt($ handle,CURLOPT_RETURNTRANSFER,true);

    $ response = curl_exec($ handle);

    curl_close($处理);

    print_r(json_decode($ response,true));

结果:

Array (
    [error] => Array (
            [errors] => Array (
                    [0] => Array (
                            [domain] => usageLimits
                            [reason] => ipRefererBlocked
                            [message] => There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed.
                            [extendedHelp] => https://console.developers.google.com
                        ))
            [code] => 403
            [message] => There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed.
        ))

我检查了IP数十次,尝试使用带有允许URL的浏览器密钥作为引荐来源。 没主意。

感谢管理员,他们帮助弄清楚了。 默认情况下-使用我们的ipv6地址卷曲...

因此,这里有三个选项:

  1. curl_setopt($ handle,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4);

  2. 将ipv6地址添加到允许列表

  3. 关闭服务器的ipv6

我必须更加专心并记住有关ipv6的内容:-)

有人告诉我,这是通过简单地从允许编辑IP的选项中删除所有Ip来解决的。 这使得它不太安全 ,但是此后它将接受所有IP。

http://i.stack.imgur.com/LSbTL.jpg

暂无
暂无

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

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