致命错误:Google电子书的未捕获异常“Google_Service_Exception”

时间:2015-07-29 04:03:51

标签: php google-api google-books

在PHP中,我试图通过谷歌api访问谷歌电子书,但我收到此错误。

  

致命错误:未捕获的异常'Google_Service_Exception'   消息'调用GET时出错   https://www.googleapis.com/books/v1/volumes?q=Henry+David+Thoreau&filter=free-ebooks&key=AIzaSyCc5s6WLKl9SrvlsN-8yXO6Cmd4qeAYR08:   (403)“权限不足”   /home/stealthbeta/public_html/google-api-php-client-master/src/Google/Http/REST.php:110   堆栈跟踪:#0   /home/stealthbeta/public_html/google-api-php-client-master/src/Google/Http/REST.php(62):   Google_Http_REST :: decodeHttpResponse(对象(Google_Http_Request)   对象(Google_Client))#1 [内部函数]:   Google_Http_REST :: doExecute(对象(Google_Client)   对象(Google_Http_Request))#2   /home/stealthbeta/public_html/google-api-php-client-master/src/Google/Task/Runner.php(174):   call_user_func_array(Array,Array)#3   /home/stealthbeta/public_html/google-api-php-client-master/src/Google/Http/REST.php(46):   Google_Task_Runner-> run()#4   /home/stealthbeta/public_html/google-api-php-client-master/src/Google/Client.php(593):   Google_Http_REST :: execute(Object(Google_Client),Object(Go in。)   /home/stealthbeta/public_html/google-api-php-client-master/src/Google/Http/REST.php   在线110

这是我的代码:

require_once 'src/Google/autoload.php';
require_once 'src/Google/Client.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("XXXXXX");
$client->setClientId("XXXXXXX");
$client->setClientSecret('XXXXX');
$client->setRedirectUri('XXXXX');
$client->setScopes(array('https://www.googleapis.com/auth/books'));

if (isset($_GET['code'])) {
    $client->authenticate($_GET['code']);
    $_SESSION['token'] = $client->getAccessToken();

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

if ($client->getAccessToken()) {
    $service = new Google_Service_Books($client);
    $optParams = array('filter' => 'free-ebooks');
    $results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);

    foreach ($results as $item) {
        echo $item['volumeInfo']['title'], "<br /> \n";
    }

    $_SESSION['token'] = $client->getAccessToken();
} else {
    $authUrl = $client->createAuthUrl();
}

如何解决这个问题?

0 个答案:

没有答案