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

报告相同问题?

悬赏问题

  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复