无法使用 php 访问 google 电子表格值
unable to access google spreedsheets values using php
我安装好了
google api 对于 OAuth2 php 库;这里:https://code.google.com/p/google-api-php-client/
google 电子表格 api php 库;这里: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() ...
我不知道它有多正确...
任何帮助和建议
我知道了...问题是我没有分享我的传播sheet...当我与客户电子邮件分享我的传播sheet然后我可以插入记录到所需的 sheets...现在我可以访问每个 sheet,插入,删除所有内容...
因此,如果有人遇到此问题,只需将您的 sheet 分享给您的客户电子邮件即可。
我安装好了 google api 对于 OAuth2 php 库;这里:https://code.google.com/p/google-api-php-client/ google 电子表格 api php 库;这里: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() ... 我不知道它有多正确... 任何帮助和建议
我知道了...问题是我没有分享我的传播sheet...当我与客户电子邮件分享我的传播sheet然后我可以插入记录到所需的 sheets...现在我可以访问每个 sheet,插入,删除所有内容...
因此,如果有人遇到此问题,只需将您的 sheet 分享给您的客户电子邮件即可。