简体   繁体   English

带有PHP的Google Spreadsheets API –致命错误:未捕获的异常

[英]Google Spreadsheets API with PHP – Fatal error: Uncaught exception

I'm trying to get started with the google SpreadSheet API. 我正在尝试使用Google SpreadSheet API。 I know there are a bunch of other languages but PHP is the only one I'm vaguely competent in. I keep falling at the first hurdle and getting: 我知道还有很多其他语言,但是PHP是我唯一能胜任的语言。我一直处于第一个障碍,并且得到:

Fatal error: Uncaught exception 'Google\Spreadsheet\Exception' in 
/Users/djave/Google Drive/Sites/practise/gdata/lib/Google/Spreadsheet/ServiceRequestFactory.php:48 

Stack trace: #0 /Users/djave/Google Drive/Sites/practise/gdata/lib/Google/Spreadsheet/SpreadsheetService.php(37): 
Google\Spreadsheet\ServiceRequestFactory::getInstance() 
  #1 /Users/djave/Google Drive/Sites/practise/gdata/index.php(32): Google\Spreadsheet\SpreadsheetService->getSpreadsheets() 
  #2 {main} thrown in /Users/djave/Google Drive/Sites/practise/gdata/lib/Google/Spreadsheet/ServiceRequestFactory.php on line 48

How this happens: 如何发生的:

Step 1 Download and add the folder google-api-php-client 步骤1下载并添加文件夹google-api-php-client

Next, fiddle with the code until it works 接下来,摆弄代码直到它起作用

set_include_path('lib/');

require_once 'lib/Google/Client.php';
require_once 'lib/Google/Service/Books.php';

$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("-------------------------------------");
$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";
}

Result: prints out a list of books 结果:打印出书籍清单

Step 2 Download and install php-google-spreadsheet-client 步骤2下载并安装php-google-spreadsheet-client

First off I copy the example exactly below what I have, then include all the right files until it can find everything: 首先,我将示例完全复制到现有示例的下面,然后包括所有正确的文件,直到可以找到所有内容为止:

set_include_path('lib/');

require_once 'lib/Google/Client.php';
require_once 'lib/Google/Service/Books.php';
require_once 'lib/Google/Spreadsheet/SpreadsheetService.php';
require_once 'lib/Google/Spreadsheet/ServiceRequestFactory.php';
require_once 'lib/Google/Spreadsheet/Exception.php';

$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("-------------------------------------");
$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";
}

        $spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
        $spreadsheetFeed = $spreadsheetService->getSpreadsheets();
        $spreadsheet = $spreadsheetFeed->getByTitle('MySpreadsheet');
        $worksheetFeed = $spreadsheet->getWorksheets();

But I just get 但是我得到

Fatal error: Uncaught exception 'Google\Spreadsheet\Exception' in /Users/djave/Google Drive/Sites/practise/gdata/lib/Google/Spreadsheet/ServiceRequestFactory.php:48 Stack trace: #0 /Users/djave/Google Drive/Sites/practise/gdata/lib/Google/Spreadsheet/SpreadsheetService.php(37): Google\Spreadsheet\ServiceRequestFactory::getInstance() #1 /Users/djave/Google Drive/Sites/practise/gdata/index.php(23): Google\Spreadsheet\SpreadsheetService->getSpreadsheets() #2 {main} thrown in /Users/djave/Google Drive/Sites/practise/gdata/lib/Google/Spreadsheet/ServiceRequestFactory.php on line 48

I'm doing something really stupid, right? 我做的事真愚蠢,对吗? Thanks for any help 谢谢你的帮助

Looks like you've forgotten to bootstrap the lib first: https://github.com/asimlqt/php-google-spreadsheet-client#bootstrapping 似乎您已经忘记了首先引导lib: https//github.com/asimlqt/php-google-spreadsheet-client#bootstrapping

require 'vendor/autoload.php';

use Google\Spreadsheet\DefaultServiceRequest;
use Google\Spreadsheet\ServiceRequestFactory;

$serviceRequest = new DefaultServiceRequest($accessToken);
ServiceRequestFactory::setInstance($serviceRequest);

Give edit permission to "client_email" field value in client_secret.json file by opening the actual spreadsheet, refer image. 通过打开实际的电子表格,授予对client_secret.json文件中“ client_email”字段值的编辑权限,请参考图像。 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谷歌日历 API 致命错误未捕获异常 - Google calendar API Fatal error uncaught exception 致命错误:未捕获的 Google_Service_Exception Google Sheets API PHP 错误添加行 - Fatal error: Uncaught Google_Service_Exception Google Sheets API PHP Error Adding Row PHP致命错误:带有消息的未捕获异常“异常” - PHP fatal error: Uncaught exception 'Exception' with message PHP致命错误:未捕获的异常&#39;Exception&#39; - PHP Fatal error: Uncaught exception 'Exception' 致命错误:未捕获的异常“ Google_IOException” - Fatal error: Uncaught exception 'Google_IOException' 致命错误:未捕获的异常&#39;Exception&#39;; 需要CURL PHP扩展名Google_Client.php - Fatal error:Uncaught exception 'Exception'; requires CURL PHP extension Google_Client.php 致命错误:未捕获的异常..使用php SwiftMailer - Fatal error: Uncaught exception .. using php SwiftMailer 致命错误:未捕获的SoapFault异常:PHP中的[WSDL] - Fatal error: Uncaught SoapFault exception: [WSDL] in php PHP 致命错误:未捕获的 GuzzleHttp\\Exception\\ClientException - PHP Fatal error: Uncaught GuzzleHttp\Exception\ClientException 致命错误:PHP中未捕获的Braintree异常 - Fatal Error: Uncaught Braintree Exception in PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM