1

嗨,

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

首先,我们确定速率限制是多少。它是:礼貌限制:1,000,000 个请求/天(在此处找到

要进行调用,我们使用 Google_UrlshortenerService.php(可在此处找到http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/urlshortener/index.php:)

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

为了确保我们使用的是 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>

任何人都可以帮助或过去有同样的问题。

4

0 回答 0