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

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

报告相同问题?