View file File name : GOOGLE_DRIVE_SETUP.md Content :# Google Drive OAuth Setup Guide ## Issue You're Seeing When trying to backup, you get: "Failed to connect to Google Drive: Error 401 - Request is missing required authentication credential" ## Cause This happens when: 1. Google Drive hasn't been authorized yet from the dashboard 2. OAuth credentials are incorrect 3. Redirect URI doesn't match Google Cloud Console settings ## Complete Setup Steps ### Step 1: Verify Your Google Cloud Console Credentials 1. Go to [Google Cloud Console](https://console.cloud.google.com) 2. Select your project (if you have multiple) 3. Click **APIs & Services** > **Credentials** (left sidebar) 4. Find your OAuth 2.0 Client (should show "Desktop application" or similar) 5. Click the edit icon (pencil) to view details 6. Verify you have: - **Client ID**: `270248718468-iqtrb096khdn0kd6udutvkk56h3p3fgb.apps.googleusercontent.com` ✓ - **Client Secret**: `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` ✓ ### Step 2: Verify Redirect URI Configuration In your Google Cloud Console: 1. Click on your OAuth 2.0 Client ID credential 2. Go to **URIs** section 3. Make sure **Authorized redirect URIs** includes: ``` https://backup.alkashier.com/google-drive/callback ``` **Important**: The URI must match EXACTLY with: - Your `.env` file: `GOOGLE_REDIRECT_URI=https://backup.alkashier.com/google-drive/callback` - Your domain: `https://backup.alkashier.com` (note the HTTPS) If the redirect URI is different: 1. Edit the OAuth credential in Google Cloud Console 2. Update it to: `https://backup.alkashier.com/google-drive/callback` 3. Save changes 4. Wait 1-2 minutes for changes to propagate ### Step 3: Authorize Google Drive from the Dashboard **IMPORTANT**: You must do this BEFORE running backups! 1. Open your application: `https://backup.alkashier.com` 2. Go to **Dashboard** 3. Find the **"Connect Google Drive"** button 4. Click it - you'll be redirected to Google's login page 5. Sign in with the Gmail account you want to use 6. When prompted, click **"Allow"** to grant permissions 7. You should be redirected back with a success message ### Step 4: Verify Connection Status After authorizing: 1. On the Dashboard, you should see your Gmail address displayed 2. The status should show "Connected" (or similar) 3. The button should change to "Disconnect Google Drive" ### Step 5: Now You Can Run Backups After Google Drive is connected: 1. Go to **Databases** 2. Add a database configuration if you haven't already 3. Go to **Dashboard** 4. Click **"Backup Now"** on a database 5. Check **Backup History** to see the result ## Troubleshooting ### Issue: "Invalid client" Error - Check that Client ID and Client Secret in `.env` are correct - Verify they match your Google Cloud Console credentials - No extra spaces or typos ### Issue: "Redirect URI mismatch" - Your `.env` GOOGLE_REDIRECT_URI must match Google Cloud Console exactly - Current value: `https://backup.alkashier.com/google-drive/callback` - Check for: - Missing/extra trailing slashes - HTTP vs HTTPS mismatch - Domain typos ### Issue: Still Getting 401 After Authorization 1. Check that `is_connected = true` in the database: ```bash php artisan tinker >>> \App\Models\GoogleDriveSetting::first() ``` You should see: `'is_connected' => true` 2. If `is_connected` is `false`, try authorizing again from the dashboard 3. Check the tokens are stored: ```bash php artisan tinker >>> $g = \App\Models\GoogleDriveSetting::first() >>> $g->access_token # Should show a long token string >>> $g->refresh_token # Should show a token string ``` ### Issue: "Access Denied" After Authorization - Your Gmail account may not have Google Drive enabled - Try signing in with a different Gmail account - Go back to Dashboard and click "Connect Google Drive" again to try with a different account ## Testing Authorization Flow To test if everything is working: 1. **SSH into your VPS** and run: ```bash cd /path/to/alkashier_backup php artisan tinker ``` 2. **Check if Google Drive is connected**: ```php >>> $service = new \App\Services\GoogleDriveService(); >>> $service->isConnected() # Should return: true ``` 3. **If it returns false**, you need to authorize from the dashboard first 4. **Try uploading a test file**: ```php >>> $service->uploadFile('/tmp/test.txt', 'test.txt') # Should return a file ID if successful ``` ## Environment Variables Your `.env` should have: ```env GOOGLE_CLIENT_ID=270248718468-iqtrb096khdn0kd6udutvkk56h3p3fgb.apps.googleusercontent.com GOOGLE_CLIENT_SECRET=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 GOOGLE_REDIRECT_URI=https://backup.alkashier.com/google-drive/callback ``` If you change any of these: 1. Update `.env` 2. Run: `php artisan config:clear` 3. Try authorizing again from the dashboard ## OAuth Flow Explained Here's what happens when you click "Connect Google Drive": 1. User clicks **"Connect Google Drive"** button 2. App generates Google authorization URL 3. User is redirected to Google's login page 4. User signs in and grants permissions 5. Google redirects back to: `https://backup.alkashier.com/google-drive/callback?code=AUTH_CODE` 6. App exchanges the `code` for `access_token` and `refresh_token` 7. Tokens are stored in database as `GoogleDriveSetting` 8. `is_connected` is set to `true` 9. Future backups can now use this token to upload files ## Key Points ✅ **Must do before backups**: - Click "Connect Google Drive" from dashboard - Sign in with your Gmail - Grant permissions ✅ **OAuth credentials must match**: - Google Cloud Console settings - `.env` file values ✅ **Redirect URI must match exactly**: - No typos - HTTPS not HTTP - No trailing slashes ✅ **After authorization**: - You should see your email on dashboard - Backups will automatically upload to Google Drive ## Next Steps 1. Go to your dashboard: `https://backup.alkashier.com` 2. Click "Connect Google Drive" 3. Authorize with your Gmail 4. Verify the connection shows your email 5. Try running a backup 6. Check "Backup History" to see the status --- **Still stuck?** Check the error logs: ```bash tail -50 /path/to/alkashier_backup/storage/logs/laravel.log ```