Home > Mobile >  I need migrate apps to use the latest Sheets API version V4
I need migrate apps to use the latest Sheets API version V4

Time:11-24

I am trying to get in a Google spreadsheet via the PHP API Client, but I am getting a 404->You need permission. I figure out that I use old turned-off sheets API version v3, what is couses this error 404->"You need permission"! Now I need to migrate my spreadsheet API from v3 to v4.

This is my gdata code, where I call class getspreadsheets(); :

 if(!$accessToken){
                
                $auth_url = $this->client->createAuthUrl();
                
                
            } else {
                
                try{
                    
                    $this->client->setAccessToken($accessToken);
                    $token = json_decode($accessToken);
            
                    if ($this->client->isAccessTokenExpired()) {
                        $this->client->refreshToken($token->refresh_token);
                        $tok = json_encode($this->client->getAccessToken());
                        $token = json_decode($tok);
                        $db->setQuery("Update #__breezingforms_addons_gdata set password = " . $db->quote($tok) . " Where form_id = " . intval($form_id));
                        $db->execute();
                    }
                    
                    $serviceRequest = new DefaultServiceRequest($token->access_token, $token->token_type);
                    ServiceRequestFactory::setInstance($serviceRequest);

                    $spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
                    $spreadsheetFeed = $spreadsheetService->getSpreadsheets();
                    
                }catch(Exception $ee){
                    
                  //$accessToken = null;
                    //$auth_url = $this->client->createAuthUrl();
          $error=$ee->getMessage();
                              
                              }
            }

            if($spreadsheetFeed !== null){
                foreach($spreadsheetFeed As $sheet){
                    $gdata_spreadsheets[$sheet->getId()] = $sheet->getTitle();
                }
            }
            
            if($gdata->spreadsheet_id != '' && isset( $gdata_spreadsheets[$gdata->spreadsheet_id] ) && $spreadsheetFeed !== null){

                $spreadsheet = $spreadsheetFeed->getByTitle($gdata_spreadsheets[$gdata->spreadsheet_id]);
                $worksheetFeed = $spreadsheet->getWorksheets();
                
                foreach ( $worksheetFeed as $sheet ){
                    $gdata_worksheets[$sheet->getId()] = $sheet->getTitle();
                }
                
                if($gdata->worksheet_id != '' && isset( $gdata_worksheets[$gdata->worksheet_id] )){
                    
                    $worksheet = $worksheetFeed->getByTitle($gdata_worksheets[$gdata->worksheet_id]);
                    $cellFeed = $worksheet->getCellFeed();

                    foreach($cellFeed->getEntries() as $cellEntry) {
                        
                        $row = $cellEntry->getRow();
                        $col = $cellEntry->getColumn();
                        
                        if( $row > 1 ){
                            break;
                        }
                        
                        $gdata_columns[] = $cellFeed->getCell($row, $col)->getContent();
                        
                    }
                    
                }
            }
        
        } catch(Exception $e){
            
            $error = $e->getMessage();
        }

Sheets API v3 code for getting spreadsheets is:

namespace Google\Spreadsheet;

use SimpleXMLElement;

/**

  • Spreadsheet Service.

  • @package Google

  • @subpackage Spreadsheet

  • @author Asim Liaquat [email protected] / class SpreadsheetService { /*

    • Fetches a list of spreadhsheet spreadsheets from google drive.
    • @return \Google\Spreadsheet\SpreadsheetFeed */ public function getSpreadsheets() { return new SpreadsheetFeed( ServiceRequestFactory::getInstance()->get('feeds/spreadsheets/private/full') ); }

    /**

    • Fetches a single spreadsheet from google drive by id if you decide
    • to store the id locally. This can help reduce api calls.
    • @param string $id the url of the spreadsheet
    • @return \Google\Spreadsheet\Spreadsheet */ public function getSpreadsheetById($id) { return new Spreadsheet( new SimpleXMLElement( ServiceRequestFactory::getInstance()->get('feeds/spreadsheets/private/full/'. $id) ) ); }

    /**

    • Returns a list feed of the specified worksheet.
    • @see \Google\Spreadsheet\Worksheet::getWorksheetId()
    • @param string $worksheetId
    • @return \Google\Spreadsheet\ListFeed */ public function getListFeed($worksheetId) { return new ListFeed( ServiceRequestFactory::getInstance()->get("feeds/list/{$worksheetId}/od6/private/full") ); }

    /**

    • Returns a cell feed of the specified worksheet.
    • @see \Google\Spreadsheet\Worksheet::getWorksheetId()
    • @param string $worksheetId
    • @return \Google\Spreadsheet\CellFeed */ public function getCellFeed($worksheetId) { return new CellFeed( ServiceRequestFactory::getInstance()->get("feeds/cells/{$worksheetId}/od6/private/full") ); } }

How to make migrate on v4? Any help?

CodePudding user response:

A Prequesites

and

composer require google/apiclient:^2.0
Documentation:
  • Related