谷歌缩短网址的速率限制

时间:2013-11-29 16:19:49

标签: php api google-api url-shortener google-url-shortener

HY,

我们尝试使用(谷歌网址缩短器)缩短链接列表(超过20.000),但在那里遇到速率限制问题。

首先,我们确定速率限制是什么。 它是:礼貌限制:每天1,000,000个请求(found here

要拨打电话我们使用Google_UrlshortenerService.php(可在此处找到:http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/urlshortener/index.php

首次运行后(通过每次通话之间延迟2秒后300次通话达到速率限制) 我们向google.de寻求帮助。我们发现了这个主题(https://groups.google.com/forum/#!topic/google-url-shortener/3t1BZuhnemM),其中有些人谈到在Extension的选项中启用“授予访问权限”,但我们在新的Google云界面中找不到此选项。

为了确保我们使用oauth2,我们将脚本与auth表单结合起来:

http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/userinfo/index.php

我们会在http://goo.gl/上看到我们的请求,但在超过300次请求后,我们仍会收到速率限制错误。

确切的错误是:

  ["domain"]=>
  string(11) "usageLimits"
  ["reason"]=>
  string(17) "rateLimitExceeded"
  ["message"]=>
  string(19) "Rate Limit Exceeded"
  ["message:protected"]=>
  string(136) "Error calling POST https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyBtn-06nR_eOOjfOPJkHtbT0oBCen6tRkQ: (403) Rate Limit Exceeded"

代码:

<?php
session_start();
set_time_limit(0);

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';
require_once 'google-api-php-client/src/contrib/Google_UrlshortenerService.php';

try{
$client = new Google_Client();
$client->setApplicationName("ekomiurlshort");
$client->setDeveloperKey("our_developer_key");

$client->setClientId("our_client_id");
$client->setClientSecret("our_client_secret");
$client->setRedirectUri("out app url");

$oauth2 = new Google_Oauth2Service($client);


$service = new Google_UrlshortenerService($client);

if (isset($_REQUEST['logout'])) {
    unset($_SESSION['access_token']);
}

if (isset($_GET['code'])) {
    $client->authenticate($_GET['code']);
    $_SESSION['token'] = $client->getAccessToken();
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
    return;
}

if (isset($_SESSION['token'])) {
    $client->setAccessToken($_SESSION['token']);
}

if (isset($_REQUEST['logout'])) {
    unset($_SESSION['token']);
    $client->revokeToken();
}


if ($client->getAccessToken()) {

    if (($handle = fopen("createdlinks.txt", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
             // Start to make API requests.
            $url = new Google_Url();
            $url->longUrl = $data[1];
            $short = $service->url->insert($url);

            file_put_contents('out/out.txt',$data[0].';'.implode(';',$short).PHP_EOL,FILE_APPEND);
            sleep(2);
        }
        fclose($handle);
    }

    // The access token may have been updated lazily.
    $_SESSION['token'] = $client->getAccessToken();
} else {
    $authUrl = $client->createAuthUrl();
}


}catch(Exception $e){
    ?><pre><?var_dump($e);?></pre><?
}
?>

<a class='login' href='<?php print $authUrl; ?>'>Connect Me!</a>

过去任何人都可以提供帮助或遇到同样的问题。

0 个答案:

没有答案