asimlqt / php-google-spreadsheet-client

A PHP library for accessing and manipulating Google Spreadsheets
Other
544 stars 152 forks source link

'Access token is invalid' error in spreadsheet library php #176

Open johnnash03 opened 7 years ago

johnnash03 commented 7 years ago

I need to upload spreadsheet via google drive in cakephp.I am using google-api-php-client to generate access token and php-google-spreadsheet-client to access the sheets.Code is as follows:

` function test() { require_once '../vendors/google-api-php-client-2.1.1/vendor/autoload.php'; $client = new \Google_Client(); $client->setApplicationName("spreadsheet"); $client->setDeveloperKey("//My developerkey"); $client = new Google_Client(); $client->setAuthConfig('client_id.json'); if( !isset($_GET['code']) ) {

    $client->addScope(Google_Service_Drive::DRIVE);
    $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/test' );
    $auth_url = $client->createAuthUrl();
    header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {

    $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/test' );
    $auth_url = $client->createAuthUrl();
    $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
    $client->setAccessToken($token);

    $serviceRequest = new Google\Spreadsheet\DefaultServiceRequest($token['access_token']);
    Google\Spreadsheet\ServiceRequestFactory::setInstance($serviceRequest);
    $spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
    $spreadsheetFeed = $spreadsheetService->getSpreadsheetFeed();
    $spreadsheet = $spreadsheetFeed->getByTitle('Test');
}

`

But I get the error that access token is invalid as shown in following image:

gmdby

What is it that I am doing wrong?