I’m trying to access a Google Sheet from my PHP application but running into authentication problems. I’ve followed the setup steps but keep getting errors when trying to fetch the spreadsheet data.
My setup process:
- Created a project in Google Cloud Console
- Enabled the necessary APIs
- Generated OAuth credentials
- Downloaded the PHP client library
- Got the spreadsheet ID from the sharing settings
Here’s my code:
<?php
session_start();
require_once 'vendor/google/Google_APIClient.php';
require_once 'vendor/google/service/Google_SheetsService.php';
define('SHEETS_APP_ID', 'your-client-id-here');
define('SHEETS_APP_SECRET', 'your-client-secret-here');
define('SHEETS_CALLBACK_URL', 'your-redirect-url-here');
define('API_SCOPE_SHEETS', 'https://www.googleapis.com/auth/spreadsheets');
define('API_SCOPE_READONLY', 'https://www.googleapis.com/auth/spreadsheets.readonly');
define('SPREADSHEET_ID', 'your-sheet-id-from-url');
$apiClient = new Google_APIClient();
$apiClient->setAppId(SHEETS_APP_ID);
$apiClient->setAppSecret(SHEETS_APP_SECRET);
$apiClient->setCallbackUri(SHEETS_CALLBACK_URL);
$apiClient->setPermissions(array(API_SCOPE_SHEETS, API_SCOPE_READONLY));
try {
$document = $sheetsService->spreadsheets->get(SPREADSHEET_ID);
echo "Name: " . $document->getTitle();
echo "Type: " . $document->getMimeType();
} catch (Exception $error) {
echo "Request failed: " . $error->getMessage();
}
?>
The error I’m getting:
Request failed: Error calling GET https://www.googleapis.com/drive/v2/files: (403) Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.
The authentication part seems to be missing but I’m not sure how to implement it properly. Any ideas what I’m missing here?