在 PHP 中,我试图通过 google api 访问 google 电子书,但我收到了这个错误。
致命错误:未捕获的异常“Google_Service_Exception”和消息“调用 GET 时出错” https://www.googleapis.com/books/v1/volumes?q=Henry+David+Thoreau&filter=free-ebooks&key=AIzaSyCc5s6WLKl9SrvlsN-8yXO6Cmd4qeAYR08: (403) Insufficient Permission' in /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(Object(Google_Http_Request), Object(Google_Client)) #1 [内部函数]: Google_Http_REST::doExecute(Object( Google_Client), Object(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),对象(在第 110 行进入 /home/stealthbeta/public_html/google-api-php-client-master/src/Google/Http/REST.php
这是我的代码:
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();
}
如何解决这个问题?