【问题标题】:Google Translate API returns 403 (PHP)谷歌翻译 API 返回 403 (PHP)
【发布时间】:2015-04-04 04:32:39
【问题描述】:

问题是关于 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($handle);

    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 的浏览器密钥作为引用。 没有想法。

【问题讨论】:

    标签: php google-api google-translate


    【解决方案1】:

    有人告诉我,只需从编辑允许的 IP 选项中删除所有 Ip 即可解决此问题。这使得它安全性降低,但之后它将接受所有 IP。

    【讨论】:

    • 对不起。这种“不太安全”可能会伤害我的钱包:-)
    • 虽然是一个有效的答案,但不推荐..我不会投反对票..但我也不会投赞成票。
    【解决方案2】:

    感谢管理员,他们帮助解决了问题。 默认情况下 - curl 使用我们的 ipv6 地址...

    所以我们在这里有三个选择:

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

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

    3. 关闭服务器的 ipv6

    我必须更加专心并记住 ipv6 :-)

    【讨论】:

    • 感谢您提供详细信息。奇怪,但“作为引荐来源网址”应该可以工作......但更新需要时间。
    猜你喜欢
    • 2012-01-26
    • 2017-04-29
    • 2011-06-06
    • 2019-09-23
    • 2011-11-07
    • 2011-12-26
    • 2012-08-06
    • 2016-09-10
    • 1970-01-01
    相关资源
    最近更新 更多