← back to Designer Wallcoverings

DW-Agents/dw-agents/GOOGLE-DRIVE-SETUP.md

137 lines

# Google Drive Authentication Setup

Your Google Drive files are not appearing because the OAuth token lacks proper scopes. Here are **3 solutions** to fix this:

---

## ✅ **OPTION 1: Regenerate OAuth Token (Quickest)**

### Steps:
1. Run the token generator:
   ```bash
   cd /root/DW-Agents
   npx tsx setup-google-drive-auth.ts
   ```

2. Open the URL shown in your browser

3. Sign in with your Google account and authorize

4. Copy the authorization code from the redirect URL

5. Paste it into the terminal

6. Copy the new `GOOGLE_REFRESH_TOKEN` to `/root/DW-Agents/.env`

7. Restart the service:
   ```bash
   pm2 restart dw-digital-samples
   ```

---

## 🔐 **OPTION 2: Create Service Account (Most Reliable)**

Service accounts are better for server-to-server access and don't expire like OAuth tokens.

### Steps:

1. **Go to Google Cloud Console:**
   - Visit: https://console.cloud.google.com/

2. **Select or create a project**

3. **Enable Google Drive API:**
   - Go to "APIs & Services" > "Library"
   - Search for "Google Drive API"
   - Click "Enable"

4. **Create Service Account:**
   - Go to "IAM & Admin" > "Service Accounts"
   - Click "Create Service Account"
   - Name: `dw-digital-samples-drive`
   - Click "Create and Continue"
   - Skip role assignment (click "Continue")
   - Click "Done"

5. **Create Key:**
   - Click on the service account email
   - Go to "Keys" tab
   - Click "Add Key" > "Create new key"
   - Choose "JSON" format
   - Download the key file

6. **Upload to server:**
   - Save the JSON file as `/root/DW-Agents/google-drive-service-account.json`

7. **Share Drive folder with service account:**
   - Open your Google Drive folder containing DIG-* files
   - Click "Share"
   - Add the service account email (looks like: `name@project-id.iam.gserviceaccount.com`)
   - Give "Viewer" permission

8. **Update the code to use service account** (see next section)

---

## 🔧 **OPTION 3: Fix Existing OAuth Scopes**

If you want to keep using OAuth but just fix the scopes:

### Update Google Cloud Console:
1. Go to https://console.cloud.google.com/
2. Navigate to "APIs & Services" > "Credentials"
3. Find your OAuth 2.0 Client ID: `691474590551-a3bm0ckfoi7lnc0hk0vbuqhkcsv9pru7`
4. Verify these scopes are enabled:
   - `https://www.googleapis.com/auth/drive.readonly`
   - `https://www.googleapis.com/auth/drive.file`
   - `https://www.googleapis.com/auth/drive.metadata.readonly`

### Then regenerate token:
```bash
cd /root/DW-Agents
npx tsx setup-google-drive-auth.ts
```

---

## 🧪 **Testing**

After setup, test Drive access:
```bash
curl -s http://45.61.58.125:9879/api/drive/search?query=DIG | jq '.'
```

Should return:
```json
{
  "success": true,
  "files": [...array of files...],
  "total": <number>
}
```

---

## 📝 **Which Option Should You Choose?**

- **Quick fix for now?** → Option 1 (OAuth regeneration)
- **Long-term reliable solution?** → Option 2 (Service Account)
- **Current setup broken?** → Option 3 (Fix existing OAuth)

**Recommended:** Option 2 (Service Account) - it's more secure and doesn't expire.

---

## 🆘 **Current Status**

**Error:** `Request had insufficient authentication scopes`

**Cause:** The current `GOOGLE_REFRESH_TOKEN` doesn't have Drive API access

**Impact:** Digital sample files from Google Drive are not appearing next to SKUs in the dashboard

**Files Ready:**
- `/root/DW-Agents/setup-google-drive-auth.ts` - OAuth token generator
- `/root/DW-Agents/GOOGLE-DRIVE-SETUP.md` - This guide