首页 > 解决方案 > 将 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();

标签: phpapigoogle-sheetsgoogle-cloud-platformgoogle-sheets-api

解决方案


推荐阅读