View file File name : DIAGNOSE_GOOGLE_DRIVE.md Content :# Google Drive Diagnosis - Run These Commands The issue is that after authorizing, the tokens aren't being retrieved properly when you try to backup. ## Quick Diagnostic SSH into your VPS and run these commands: ```bash cd /path/to/alkashier_backup php artisan tinker ``` Then paste these commands: ```php // Check if Google Drive settings exist >>> $setting = \App\Models\GoogleDriveSetting::first() >>> if ($setting) { echo "Found settings record:\n"; echo "ID: {$setting->id}\n"; echo "Connected: " . ($setting->is_connected ? 'YES' : 'NO') . "\n"; echo "Email: {$setting->email}\n"; echo "Access Token: " . (strlen($setting->access_token ?? '') > 0 ? 'YES (' . strlen($setting->access_token) . ' chars)' : 'NO') . "\n"; echo "Refresh Token: " . (strlen($setting->refresh_token ?? '') > 0 ? 'YES (' . strlen($setting->refresh_token) . ' chars)' : 'NO') . "\n"; echo "Token Expires: {$setting->token_expires_at}\n"; } else { echo "NO settings found - you haven't authorized yet\n"; } ``` This will show you what's stored in the database. ## Expected Output After Authorization ``` Found settings record: ID: 1 Connected: YES Email: your.email@gmail.com Access Token: YES (1234 chars) Refresh Token: YES (456 chars) Token Expires: 2024-12-03 23:45:00 ``` ## What to Check ### If you see "NO settings found" - You haven't clicked "Connect Google Drive" yet - Go to dashboard and click the button ### If Connected is "NO" - Authorization failed - Try clicking "Connect Google Drive" again ### If Access Token is "NO" - Tokens weren't saved - Check storage/logs/laravel.log for errors - Try authorizing again ### If Refresh Token is "NO" - Google didn't provide a refresh token (happens on re-auth) - This is the likely cause of your 401 error - **Solution below** ## The Real Problem When you authorize **for the second time**, Google doesn't always return a refresh token. This causes backups to fail. The fix I've implemented: 1. First auth gets both access + refresh tokens ✓ 2. Refresh token is stored and reused ✓ 3. If refresh fails, logs show the error ✓ ## Check the Logs After trying to backup, check the logs: ```bash # On your VPS: tail -100 /path/to/alkashier_backup/storage/logs/laravel.log # Look for lines like: # "Google Drive authenticated" # "Google Drive upload failed" # "Failed to refresh Google Drive token" ``` ## Try Again After these fixes, try: 1. **Disconnect** Google Drive (Dashboard button) 2. **Wait 5 seconds** 3. **Connect** Google Drive again 4. **Sign in** with Gmail 5. **Grant permissions** 6. **Try a backup** Check logs to see what happens: ```bash tail -50 storage/logs/laravel.log | grep -i google ``` ## If Still Getting 401 The error "Request is missing required authentication credential" means: 1. `is_connected` is false (not authorized) 2. `access_token` is empty (token not saved) 3. Token expired and refresh failed Run the diagnostic above to see which one it is. ## Force Re-authorization If tokens are corrupted, clear them: ```bash php artisan tinker >>> \App\Models\GoogleDriveSetting::truncate() >>> exit ``` Then go to dashboard and authorize again. ## Test Upload After Fix Once tokens are saved, test with: ```bash php artisan tinker >>> $service = new \App\Services\GoogleDriveService() >>> $service->isConnected() # Should return: true >>> $service->uploadFile('/tmp/test.sql', 'test.sql') # Should return: file ID or error message ``` --- **Run the diagnostic above and let me know what you see!**