asimlqt / php-google-spreadsheet-client

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

You need permission Error #185

Open nickwoelk opened 6 years ago

nickwoelk commented 6 years ago

When I run my program I get this error. Fatal error: Uncaught exception 'Google\Spreadsheet\Exception\UnauthorizedException' with message 'You need permission' in /Users/nickwoelk/Desktop/migration_form_to_sheets/vendor/asimlqt/php-google-spreadsheet-client/src/Google/Spreadsheet/DefaultServiceRequest.php:356

I've already granted permissions to the spreadsheet and am able to read and create a new row. But it won't write my values to the spreadsheet.

`<?php require DIR . '/vendor/autoload.php'; use Google\Spreadsheet\ServiceRequestFactory; use Google\Spreadsheet\DefaultServiceRequest;

// creates a new Google Access Token putenv('GOOGLE_APPLICATION_CREDENTIALS=' . DIR . '/client_secret.json'); $client = new Google_Client; $client->useApplicationDefaultCredentials(); $client->setApplicationName("Migration form to Sheets"); $client->setScopes(['https://www.googleapis.com/auth/drive','https://spreadsheets.google.com/feeds']); if ($client->isAccessTokenExpired()) { $client->refreshTokenWithAssertion(); } $accessToken = $client->fetchAccessTokenWithAssertion()["access_token"]; $serviceRequest = new DefaultServiceRequest($accessToken); ServiceRequestFactory::setInstance($serviceRequest);

// Get our spreadsheet $spreadsheet = (new Google\Spreadsheet\SpreadsheetService) ->getSpreadsheetFeed() ->getByTitle('Churn');

// Get the first worksheet (tab) $worksheets = $spreadsheet->getWorksheetFeed()->getEntries(); $worksheet = $worksheets[0];

$listFeed = $worksheet->getListFeed();

// Inserts newdata into new row $listFeed->insert([ 'migration_date' => '', 'store_url' => 'mystore.myshopify.com', 'ticket_link' => '', 'subscriber_count' => "200", 'variation_of_active_customers' => '', 'percentage' => '', 'coming_from' => '', 'why_are_they_migrating' => '', //... ]);

?>`