Skip to main content

Google Drive Integration

Enable Google Drive integration to import medical documents and export diagnosis reports directly to Google Drive.

Overview

The Google Drive integration allows users to:

  • Import medical documents from Google Drive
  • Export diagnosis reports to Google Drive
  • Sync case files with Google Drive

Prerequisites

Before setting up Google Drive integration, you need:

  1. Google Cloud Project: Create a project in Google Cloud Console
  2. OAuth 2.0 Credentials: Set up OAuth 2.0 credentials
  3. Google Drive API: Enable the Google Drive API
  4. Client ID: Obtain your OAuth 2.0 client ID

Setup Instructions

Step 1: Create Google Cloud Project

  1. Go to Google Cloud Console
  2. Click "Select a project" → "New Project"
  3. Enter project name (e.g., "My Medical App")
  4. Click "Create"

Step 2: Enable Google Drive API

  1. In Google Cloud Console, go to "APIs & Services" → "Library"
  2. Search for "Google Drive API"
  3. Click "Enable"

Step 3: Create OAuth 2.0 Credentials

  1. Go to "APIs & Services" → "Credentials"
  2. Click "Create Credentials" → "OAuth client ID"
  3. If prompted, configure the OAuth consent screen:
    • User Type: External (or Internal for Google Workspace)
    • App name: Your application name
    • User support email: Your email
    • Developer contact: Your email
    • Click "Save and Continue"
  4. Add scopes (optional for testing):
    • Click "Add or Remove Scopes"
    • Add https://www.googleapis.com/auth/drive.file
    • Click "Update" → "Save and Continue"
  5. Add test users (for external apps in testing):
    • Add email addresses of users who can test
    • Click "Save and Continue"
  6. Return to "Credentials"
  7. Click "Create Credentials" → "OAuth client ID"
  8. Application type: "Web application"
  9. Name: "Knidian SDK Integration"
  10. Authorized JavaScript origins:
    • Add your application URLs:
      https://yourdomain.com
      http://localhost:3000 (for development)
  11. Authorized redirect URIs (not required for SDK, but recommended):
    https://yourdomain.com
    http://localhost:3000 (for development)
  12. Click "Create"
  13. Copy your Client ID (looks like 123456789-abc123.apps.googleusercontent.com)

Step 4: Configure SDK

Add your Google OAuth 2.0 Client ID to the SDK configuration:

const sdk = KnidianSDK.create({
authUrl: 'https://your-backend.com/api/knidian-auth',
userId: 'user-123',
container: '#app',

// Enable Google Drive integration
googleDrive: {
clientId: '123456789-abc123.apps.googleusercontent.com'
}
});

Step 5: Configure Environment Variables

Store your Client ID securely:

# .env file
GOOGLE_CLIENT_ID=123456789-abc123.apps.googleusercontent.com
// Use in your app
const sdk = KnidianSDK.create({
authUrl: 'https://your-backend.com/api/knidian-auth',
userId: currentUser.id,
container: '#app',
googleDrive: {
clientId: process.env.GOOGLE_CLIENT_ID
}
});

Usage

Importing Documents from Google Drive

Once configured, users can import documents directly from their Google Drive:

  1. Click the "Upload" or "Import" button in the SDK
  2. Select "Import from Google Drive"
  3. Authenticate with Google (first time only)
  4. Browse and select files from Google Drive
  5. Files are imported into the current case

Exporting Diagnoses to Google Drive

Export diagnosis reports to Google Drive:

  1. Complete a diagnosis
  2. Click "Export" in the diagnosis panel
  3. Select "Export to Google Drive"
  4. Choose destination folder (or use root)
  5. File is saved as Diagnosis_Report_[CaseID].pdf or .docx

Features

Supported File Types

Import from Google Drive

  • Images: JPEG, PNG, GIF
  • Documents: PDF, DOC, DOCX
  • Medical files: DICOM (if enabled)

Export to Google Drive

  • PDF reports
  • DOCX reports
  • JSON data (if enabled)

Authentication Flow

The SDK handles Google OAuth 2.0 authentication automatically:

  1. First Use: User clicks "Connect Google Drive"
  2. Consent: Google OAuth consent screen appears
  3. Authorization: User grants permission to access Drive
  4. Token Storage: Access token stored securely in browser
  5. Auto-Refresh: Tokens refreshed automatically

Permissions Required

The SDK requests minimal permissions:

  • Scope: https://www.googleapis.com/auth/drive.file
  • Access: Only files created by the SDK
  • No Access To: User's other Google Drive files

Advanced Configuration

Custom Scopes

Request additional Google Drive permissions:

// Note: SDK currently uses drive.file scope only
// Contact support@knidian.ai for custom scope requirements

Custom Export Folder

Specify default export folder:

// This feature is currently handled by user selection
// during export. Custom default folders may be added in future releases.

Security Considerations

Client ID Security

Important: The Google OAuth Client ID is NOT a secret. It's safe to include in your client-side code.

However, for additional security:

  1. Restrict HTTP Referrers: In Google Cloud Console

    • Go to Credentials → Edit OAuth 2.0 Client ID
    • Under "Authorized JavaScript origins"
    • Limit to your production domains only
  2. Use Application Restrictions:

    • Restrict the Client ID to specific domains
    • Remove localhost before production deployment

User Data Privacy

The SDK:

  • Only accesses files it creates (via drive.file scope)
  • Does NOT access user's existing Google Drive files
  • Stores access tokens in browser localStorage
  • Automatically revokes access on logout

HIPAA Compliance

For HIPAA compliance with Google Drive:

  1. Google Workspace: Use Google Workspace (Business or Enterprise)
  2. BAA with Google: Ensure you have a Business Associate Agreement with Google
  3. Encryption: All files encrypted in transit and at rest by Google
  4. Access Logs: Enable Google Drive audit logs

Contact compliance@knidian.ai for HIPAA guidance.

Troubleshooting

Error: "Invalid Client ID"

Cause: Client ID is incorrect or not properly configured

Solution:

  1. Verify Client ID in Google Cloud Console
  2. Ensure it's copied correctly (no extra spaces)
  3. Check that Google Drive API is enabled
  4. Verify authorized JavaScript origins include your domain

Error: "Access Denied"

Cause: User denied permission or app not verified

Solution:

  1. User must click "Allow" in OAuth consent screen
  2. If app is unverified, user must click "Advanced" → "Go to [App Name] (unsafe)"
  3. For production, submit app for Google verification

Error: "Redirect URI Mismatch"

Cause: The redirect URI doesn't match configured URIs

Solution:

  1. Go to Google Cloud Console → Credentials
  2. Edit OAuth 2.0 Client ID
  3. Add your current URL to "Authorized JavaScript origins"
  4. Include both production and development URLs

Files Not Appearing in Google Drive

Cause: Files may be in "Shared with me" or different folder

Solution:

  1. Check "My Drive" in Google Drive
  2. Search for file by name (e.g., "Diagnosis_Report")
  3. Files created by SDK appear in root or selected folder

Authentication Loop

Cause: Popup blocker or localStorage issues

Solution:

  1. Disable popup blockers for your domain
  2. Clear browser localStorage
  3. Try in incognito/private mode
  4. Check browser console for errors

Testing

Development Testing

Test Google Drive integration in development:

const sdk = KnidianSDK.create({
authUrl: 'http://localhost:3000/api/knidian-auth',
userId: 'test-user',
container: '#app',
googleDrive: {
clientId: process.env.GOOGLE_CLIENT_ID // Uses localhost in dev
}
});

Remember:

  1. Add http://localhost:3000 to authorized origins
  2. Add test user emails to OAuth consent screen
  3. Use test/dev Google account

Production Checklist

Before deploying to production:

  • Google Drive API enabled
  • OAuth 2.0 Client ID created
  • Production domain added to authorized origins
  • OAuth consent screen configured
  • Client ID stored in environment variables
  • Test import functionality
  • Test export functionality
  • Verify files appear in Google Drive
  • Test with multiple users
  • Remove localhost from authorized origins

App Verification (Production)

For production apps with many users, Google requires app verification.

When Verification is Required

  • External users (non-Google Workspace)
  • More than 100 users
  • Sensitive or restricted scopes

Verification Process

  1. Go to Google Cloud Console
  2. Navigate to "OAuth consent screen"
  3. Click "Publish App"
  4. Submit for verification:
    • Company homepage
    • Privacy policy URL
    • Terms of service URL
    • App logo
    • App verification video (if required)
  5. Wait for Google review (1-3 weeks typically)

Note: Apps in testing mode can have up to 100 test users without verification.

API Quotas

Google Drive API has usage quotas:

  • Queries per 100 seconds: 1,000
  • Queries per day: 1,000,000,000

These limits are more than sufficient for typical SDK usage.

Monitor Usage

  1. Go to Google Cloud Console
  2. Navigate to "APIs & Services" → "Dashboard"
  3. Click "Google Drive API"
  4. View usage metrics

Examples

Complete Setup Example

// .env
GOOGLE_CLIENT_ID=123456789-abc123.apps.googleusercontent.com

// app.js
const sdk = KnidianSDK.create({
authUrl: 'https://your-backend.com/api/knidian-auth',
userId: currentUser.id,
container: '#knidian-container',

// Enable Google Drive
googleDrive: {
clientId: process.env.GOOGLE_CLIENT_ID
}
});

console.log('SDK initialized with Google Drive support');

Monitoring Google Drive Usage

Coming Soon

Event callbacks (onFileUploaded, onError) are not yet available. Track Google Drive usage through your backend logs.

Current approach:

const sdk = KnidianSDK.create({
authUrl: 'https://your-backend.com/api/knidian-auth',
userId: currentUser.id,
container: '#app',

googleDrive: {
clientId: process.env.GOOGLE_CLIENT_ID
}
});

// Users can import/export files via the SDK UI
// Track usage through your backend auth endpoint logs

Disabling Google Drive

To disable Google Drive integration, simply omit the googleDrive configuration:

const sdk = KnidianSDK.create({
authUrl: 'https://your-backend.com/api/knidian-auth',
userId: 'user-123',
container: '#app'
// No googleDrive config = disabled
});

Users will not see Google Drive import/export options.

Best Practices

  1. Use Environment Variables: Never hardcode Client ID in source code committed to version control
  2. Restrict Origins: Limit authorized origins to your actual domains
  3. Test Thoroughly: Test import and export with various file types
  4. Handle Errors: Implement proper error handling for OAuth failures
  5. User Education: Provide help text explaining Google Drive permissions
  6. Monitor Usage: Track Google Drive API usage in Google Cloud Console
  7. Security: Review and update OAuth consent screen information regularly

Future Enhancements

Planned features:

  • Auto-sync case files to Google Drive
  • Folder organization options
  • Batch export
  • Import from Google Sheets (structured data)

Contact features@knidian.ai to request specific Google Drive features.

Support

For Google Drive integration issues:

Next Steps