Spaces:
Sleeping
Sleeping
File size: 3,073 Bytes
6a725a4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | # Google Drive Hard-Coded Auth Guide
Use this guide to enable automatic Drive uploads without running OAuth each time.
## Option 1: Refresh Token (recommended, simple)
### Step 1: Obtain a refresh token
1. In a Python shell run:
```python
from google_drive_sync import get_refresh_token
refresh_token = get_refresh_token()
```
2. A browser window opens for Google sign-in
3. The terminal prints your `refresh_token`, e.g.
```
REFRESH_TOKEN = "1//0gxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
```
### Step 2: Configure the refresh token
Open `google_drive_sync.py` and set:
```python
REFRESH_TOKEN = None # place your refresh token here
```
Replace `None` with your token:
```python
REFRESH_TOKEN = "1//0gxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # your refresh token
```
### Step 3: Done
Save the file; future uploads happen automatically without another login.
---
## Option 2: Service Account (more secure, production-friendly)
### Step 1: Create the Service Account
1. Visit [Google Cloud Console](https://console.cloud.google.com/)
2. Select or create a project
3. Enable the Google Drive API
4. Go to “IAM & Admin” → “Service Accounts”
5. Click “Create Service Account”
6. Provide a name/description, click “Create and Continue”
7. Grant the “Editor” role, then “Continue” → “Done”
### Step 2: Create and download the key
1. Open the Service Account
2. Go to the “Keys” tab
3. Click “Add Key” → “Create new key”
4. Choose JSON → “Create”
5. Save the JSON into your project directory (e.g., `service-account-key.json`)
### Step 3: Share the Drive folder with the Service Account
1. Create a folder in Drive (e.g., “Chatbot_Data”)
2. Right-click → “Share”
3. In the JSON file find `client_email` (looks like `xxx@xxx.iam.gserviceaccount.com`)
4. Add that email to the folder with “Editor” access
### Step 4: Point the code to the Service Account file
Set this in `google_drive_sync.py`:
```python
SERVICE_ACCOUNT_FILE = None # e.g. "path/to/service-account-key.json"
```
Replace `None` with your JSON path:
```python
SERVICE_ACCOUNT_FILE = "service-account-key.json" # your Service Account JSON path
```
### Step 5: Done
All data now syncs to the folder you shared with the Service Account!
---
## Verify the setup
Run the app and confirm uploads reach Drive. If not, check:
1. Refresh token or Service Account path is correct
2. Network connectivity
3. Google Drive API is enabled
4. Service Account has access to the folder
## Notes
⚠️ **Security reminders**
- Never commit credentials to public repos
- Add `google_drive_sync.py` or secrets to `.gitignore` if needed
- Protect the Service Account JSON carefully
## Troubleshooting
### Refresh token expired
Re-run `get_refresh_token()` to obtain a new one.
### Service Account can’t access the folder
Ensure the Service Account email was invited with the proper permission.
### Authentication failure
1. Double-check `CLIENT_ID` / `CLIENT_SECRET`
2. Confirm the Drive API is enabled
3. Verify your network connection
|