无法使用php

时间:2015-06-25 15:12:46

标签: php google-sheets google-spreadsheet-api google-api-php-client

我已经安装好了     用于OAuth2 php lib的google api;在这里:https://code.google.com/p/google-api-php-client/     谷歌电子表格api php lib;在这里:https://github.com/asimlqt/php-google-spreadsheet-client

使用服务帐户https://console.developers.google.com/

在API控制台上创建凭据

这是我与上面结合使用的php脚本:

require_once('vendor/autoload.php'); 
require_once('vendor/composer/autoload_classmap.php'); 
require_once('vendor/composer/autoload_real.php'); 
require_once __DIR__.'/vendor/google/apiclient/src/Google/Auth/AssertionCredentials.php';

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

const G_CLIENT_ID       = 'my_client_id';
const G_CLIENT_EMAIL    = 'email address';
const G_CLIENT_KEY_PATH = 'key.p12';
const G_CLIENT_KEY_PW   = 'notasecret';


$obj_client_auth  = new Google_Client ();
$obj_client_auth -> setApplicationName ('newproject');
$obj_client_auth -> setClientId (G_CLIENT_ID);
$obj_client_auth -> setAssertionCredentials (new Google_Auth_AssertionCredentials (
    G_CLIENT_EMAIL, 
    array('https://spreadsheets.google.com/feeds','https://docs.google.com/feeds'), 
    file_get_contents (G_CLIENT_KEY_PATH), 
    G_CLIENT_KEY_PW
));
$obj_client_auth -> getAuth () -> refreshTokenWithAssertion ();
$obj_token  = json_decode ($obj_client_auth -> getAccessToken ());
$accessToken = $obj_token->access_token;
$serviceRequest = new DefaultServiceRequest($accessToken);
ServiceRequestFactory::setInstance($serviceRequest);
$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheets();
$spreadsheet = $spreadsheetFeed->getByTitle('NewSpreadSheet');
$worksheetFeed = $spreadsheet->getWorksheets();
$worksheet = $worksheetFeed->getByTitle('sssNew Worksheet');
$listFeed = $worksheet->getListFeed();
$row = array('name'=>'John', 'age'=>25);

我收到以下错误:     在非对象上调用成员函数getWorksheets()...     我没有弄到多么正确......     任何帮助和建议

1 个答案:

答案 0 :(得分:3)

我明白了...问题在于我没有分享我的电子表格...当我用客户端电子邮件分享我的电子表格时,我能够将记录插入到所需的表单中......现在我可以访问每个工作表,插入,删除所有内容......

因此,当有人遇到此问题时,只需与您的客户电子邮件共享您的工作表。