(403)使用google-api-php-client时,未经身份验证使用超过每日限制



我的目标是在google fusion表API上创建最简单的SQL查询。为此,我安装了google-api-php-client,并尝试了下面的代码:

    $client_id = '<client_id>';
    $client_secret = '<client_secret>';
    $redirect_uri = '<redirecti_uri>';
    $client = new Google_Client();
    $client->setClientId($client_id);
    $client->setClientSecret($client_secret);
    $client->setRedirectUri($redirect_uri);
    //$client->setScopes('email');
    $client->setScopes(
        array(
            'https://www.googleapis.com/auth/fusiontables',
            'https://www.googleapis.com/auth/fusiontables.readonly',
        )
    );
    $service = new Google_Service_Fusiontables($client);
    $sql = 'select name from <my_table>';
    $result = $service->query->sql($sql);
    return $this->render("MinnAdsBundle:Motors:test3.html.twig", 
            array('result'=>  var_dump($result)));

我得到这个错误:

Error calling POST https://www.googleapis.com/fusiontables/v1/query: (403) Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.
500 Internal Server Error - Google_Service_Exception

有什么想法来获取查询的验证吗?

谢谢

此时不能查询服务,身份验证还没有完成(您已经在代码中提交了完整的身份验证过程)。

修改了user-example.php(有两个以//!!!开头的注释来标记显著的变化):

<?php
/*
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
include_once "templates/base.php";
session_start();
set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Service/Fusiontables.php';
/************************************************
  ATTENTION: Fill in these values! Make sure
  the redirect URI is to this page, e.g:
  http://localhost:8080/user-example.php
 ************************************************/
 $client_id     = '<YOUR_CLIENT_ID>';
 $client_secret = '<YOUR_CLIENT_SECRET>';
 $redirect_uri  = '<YOUR_REDIRECT_URI>';
/************************************************
  Make an API request on behalf of a user. In
  this case we need to have a valid OAuth 2.0
  token for the user, so we need to send them
  through a login flow. To do this we need some
  information from our API console project.
 ************************************************/
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
 $client->setScopes(
        array(
            'https://www.googleapis.com/auth/fusiontables',
            'https://www.googleapis.com/auth/fusiontables.readonly',
        )
    );
/************************************************
  When we create the service here, we pass the
  client to it. The client then queries the service
  for the required scopes, and uses that when
  generating the authentication URL later.
 ************************************************/
//!!!here only create the service
$service = new Google_Service_Fusiontables($client);

/************************************************
  If we're logging out we just need to clear our
  local access token in this case
 ************************************************/
if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
}
/************************************************
  If we have a code back from the OAuth 2.0 flow,
  we need to exchange that with the authenticate()
  function. We store the resultant access token
  bundle in the session, and redirect to ourself.
 ************************************************/
if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
/************************************************
  If we have an access token, we can make
  requests, else we generate an authentication URL.
 ************************************************/
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);
} else {
  $authUrl = $client->createAuthUrl();
}
/************************************************
  If we're signed in and have a request to shorten
  a URL, then we create a new URL object, set the
  unshortened URL, and call the 'insert' method on
  the 'url' resource. Note that we re-store the
  access_token bundle, just in case anything
  changed during the request - the main thing that
  might happen here is the access token itself is
  refreshed if the application has offline access.
 ************************************************/
if ($client->getAccessToken() ) {
    //!!!here send the query and do something with $result
    $sql = 'select * from <my_table>';
    $result = $service->query->sql($sql);
  $_SESSION['access_token'] = $client->getAccessToken();
}
echo pageHeader("User Query - Fusiontables");
if (
    $client_id == '<YOUR_CLIENT_ID>'
    || $client_secret == '<YOUR_CLIENT_SECRET>'
    || $redirect_uri == '<YOUR_REDIRECT_URI>') {
  echo missingClientSecretsWarning();
}
?>
<div class="box" style="width:80%">
  <div class="request">
    <?php if (isset($authUrl)): ?>
      <a class='login' href='<?php echo $authUrl; ?>'>Connect Me!</a>
    <?php endif ?>
  </div>
  <?php if (isset($result)): ?>
    <pre style="overflow:auto;height:500px;">
      <?php echo htmlentities(print_r($result,true)); ?>
    </pre>
  <?php endif ?>
</div>

相关内容

最新更新