function initiateGoogleConnection()
{
    global $path;
    session_start();
    $client = generateClient(false);
    if (isset($_GET['code'])) {
        $client->authenticate($_GET['code']);
        $writeFile = fopen($path . "accessToken.json", "w");
        fwrite($writeFile, $client->getAccessToken());
        # save $client->getAccessToken() to a file
        exit;
    }
    echo '<a href="' . $client->createAuthUrl() . '">Authenicate</a>';
}
<?php

$path = $_SERVER['DOCUMENT_ROOT'] . '//';
require "vendor/autoload.php";
require $path . "GoogleAuthenticationHandler.php";
use Google\Spreadsheet\DefaultServiceRequest;
use Google\Spreadsheet\ServicerequestFactory;
use Google\Spreadsheet\SpreadsheetService;
$infoFile = fopen($path . "spreadsheetInfo.json", "r");
$info = json_decode(fread($infoFile, filesize($path . "spreadsheetInfo.json")), true);
$client = generateClient(true);
$serviceRequest = new DefaultServiceRequest(json_decode($client->getAccessToken(), true)["access_token"]);
ServiceRequestFactory::setInstance($serviceRequest);
$spreadsheetService = new SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheets();
$sheet = $spreadsheetFeed->getByTitle($info['spreadsheetName']);
$worksheetFeed = $sheet->getWorksheets();
$worksheet = $worksheetFeed->getByTitle($info['worksheetName']);
$listFeed = $worksheet->getListFeed();
$listFeed->insert($_POST);
header('Location: testIndex.php');