dounielong7728 2015-02-04 15:24
浏览 79
已采纳

Google Translate API返回403(PHP)

The question is about Google Translate API

  1. I set a project and turn the billing on (with some money on the account)

  2. I created a key for server app (also tryed to use a browser key) and added all 3 IPs I have (home and 2 servers)

What I see:

  1. It does work in apis-explorer and in a browser address bar (https://www.googleapis.com/language/translate/v2/detect?q=an%20english%20text&key=MY_KEY)

  2. It returns 403 error if I trying to get the same URL from PHP code:

    $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));

thre result:

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.
        ))

I checked the IPs dozens of times, tryed to use browser key with allowed URL as referer. Out of ideas.

  • 写回答

2条回答 默认 最新

  • ds9567 2015-02-07 20:30
    关注

    Thanks for admins, they helped to figure out. By default - curl using our ipv6 address...

    So we have three options here:

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

    2. add ipv6 address to allowed list

    3. Turn ipv6 off forthe server

    I had to be more attentive and remember about ipv6 :-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿