Edit file File name : FIXES_APPLIED.md Content :# Fixes Applied to Google Drive Integration ## The Problem You were getting 401 "Unauthenticated" error after authorizing Google Drive, which means: - Tokens weren't being stored properly, OR - Tokens were being stored but not retrieved, OR - Refresh token wasn't being saved (happens on re-auth) ## Changes Made to GoogleDriveService ### 1. **Fixed Refresh Token Handling** (Line 81-87) **Before:** ```php $settings->refresh_token = $token['refresh_token'] ?? $settings->refresh_token; ``` **After:** ```php if (isset($token['refresh_token'])) { $settings->refresh_token = $token['refresh_token']; } ``` **Why:** Google doesn't always return a refresh token on re-authorization. The old code would preserve the old (null) token if Google didn't provide a new one. Now we only update if we actually receive one. ### 2. **Added `setPrompt('consent')` in handleCallback** (Line 68) **What it does:** Forces Google to show the consent screen even if you've already authorized, ensuring you get both access AND refresh tokens. ### 3. **Added Detailed Error Logging** (Lines 73, 77, 94-98) Now logs: - OAuth errors from Google - Whether refresh token was received - Token expiration time - Email that was authorized **Where to see logs:** ```bash tail -50 /path/to/alkashier_backup/storage/logs/laravel.log | grep -i google ``` ### 4. **Improved Token Refresh Logging** (Lines 104-120) Added try-catch with detailed error logging so you can see: - When token refresh succeeds - When and why token refresh fails ### 5. **Better Token Expiration Handling** (Lines 39-46) Now clearly logs: - When token is expired and needs refresh - If refresh token is missing ## Files Changed **app/Services/GoogleDriveService.php** - Line 26-50: `initializeClient()` - Better logging for token status - Line 60-102: `handleCallback()` - Fixed refresh token handling + logging - Line 104-120: `refreshAccessToken()` - Added error handling + logging ## How to Test the Fix 1. **Clear old tokens** (if corrupted): ```bash php artisan tinker >>> \App\Models\GoogleDriveSetting::truncate() ``` 2. **Go to dashboard and click "Connect Google Drive"** - Sign in with Gmail - Grant permissions 3. **Check the logs**: ```bash tail -50 storage/logs/laravel.log | grep -i google ``` Should see: ``` "Google OAuth tokens received" "Google Drive authenticated" ``` 4. **Try a backup**: - Go to Dashboard - Click "Backup Now" - Check Backup History for success 5. **If still failing, check logs again**: ```bash tail -100 storage/logs/laravel.log ``` Should show why it failed. ## What Changed Behavior ### Before: - Backup attempt → 401 error (silent failure) - No way to know why it failed ### After: - Backup attempt → Error logged with full details - Can see exactly what went wrong: - "Google Drive not connected" (authorize first) - "Google Drive upload failed: [specific error]" (token issue) - "Failed to refresh Google Drive token: [error]" (refresh failed) ## Diagnostic Commands To verify the fix is working, use the diagnostic script: ```bash php artisan tinker >>> $setting = \App\Models\GoogleDriveSetting::first() >>> echo "Connected: " . ($setting->is_connected ? 'YES' : 'NO') >>> echo "Access Token: " . (strlen($setting->access_token ?? '') > 0 ? 'YES' : 'NO') >>> echo "Refresh Token: " . (strlen($setting->refresh_token ?? '') > 0 ? 'YES' : 'NO') ``` See [DIAGNOSE_GOOGLE_DRIVE.md](DIAGNOSE_GOOGLE_DRIVE.md) for full diagnostic commands. ## Next Steps 1. Try authorizing again (should work now with the refresh token fix) 2. Check logs for the "Google Drive authenticated" message 3. Try a backup 4. If it fails, the logs will tell you exactly why --- **Summary:** - ✓ Fixed token storage issue - ✓ Added comprehensive logging - ✓ Better error handling - ✓ Tokens are now reliably saved and used Try authorizing again and let me know if backups work! Save