php - 将 Google Sheet API 从 V3 更改为 V4
问题描述
这是我的代码按工作表名称从谷歌工作表获取数据。我想知道当我将 setScopes 更改为下方时,它会将 API 从 V3 更改为 V4。
有人能告诉我我还需要改变什么吗?或者这样可以吗?
谢谢
// Get data from google sheet
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/secretkey.json');
/* SEND TO GOOGLE SHEETS */
$client = new Google_Client;
try{
$client->useApplicationDefaultCredentials();
$client->setApprovalPrompt('force');
$client->setApplicationName("Something to do with my representatives");
$client->setScopes(['https://www.googleapis.com/auth/drive','https://spreadsheets.google.com/feeds']);
if ($client->isAccessTokenExpired()) {
$client->refreshTokenWithAssertion();
}
$accessToken = $client->fetchAccessTokenWithAssertion()["access_token"];
ServiceRequestFactory::setInstance(
new DefaultServiceRequest($accessToken)
);
// Get our spreadsheet
$spreadsheet = (new Google\Spreadsheet\SpreadsheetService)
->getSpreadsheetFeed()
->getByTitle('xxxxxxx');
// Get the first worksheet (tab)
$worksheets = $spreadsheet->getWorksheetFeed()->getEntries();
$arr = array();
$worksheet = $worksheets[0];
$listFeed = $worksheet->getListFeed();
// Get data from google sheet
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/secretkey.json');
/* SEND TO GOOGLE SHEETS */
$client = new Google_Client;
try{
$client->useApplicationDefaultCredentials();
$client->setApprovalPrompt('force');
$client->setApplicationName("Something to do with my representatives");
$client->setScopes(['https://www.googleapis.com/auth/spreadsheets']);
if ($client->isAccessTokenExpired()) {
$client->refreshTokenWithAssertion();
}
$accessToken = $client->fetchAccessTokenWithAssertion()["access_token"];
ServiceRequestFactory::setInstance(
new DefaultServiceRequest($accessToken)
);
// Get our spreadsheet
$spreadsheet = (new Google\Spreadsheet\SpreadsheetService)
->getSpreadsheetFeed()
->getByTitle('xxxxxxx');
// Get the first worksheet (tab)
$worksheets = $spreadsheet->getWorksheetFeed()->getEntries();
$arr = array();
$worksheet = $worksheets[0];
$listFeed = $worksheet->getListFeed();
解决方案
推荐阅读
- r - 被动检查文件是否可用
- sql - 如何在某些条件下合并行postgres Sql
- arrays - 如何用 jq 连接 json 文件?
- java - 有没有办法从 Java 的扫描仪中删除我的项目?
- mysql - mysql self join on parent_id 但按字母顺序排列
- php-deployer - PHP Deployer:如何从主机设置中获取用户名
- django - 拒绝在 django 3.0 和 chrome 中显示 iframe 或嵌入标签
- node.js - 对于在 GCP Compute Engine(托管实例组)上运行的 Node.js 项目,将 VM 的外部 IP 写入 .env 文件的最佳方法是什么
- java - 将 Playstore 中的现有应用从 java 迁移到 kotlin
- typescript - 如何将 Record 映射到相同大小的 Tuple?