| """ |
| Generate Google OAuth Refresh Token for Drive API |
| Run this locally to get a new refresh token, then add it to HF Space secrets |
| """ |
| import os |
| import json |
| from google_auth_oauthlib.flow import InstalledAppFlow |
| from google.auth.transport.requests import Request |
|
|
| |
| SCOPES = ['https://www.googleapis.com/auth/drive'] |
|
|
| def main(): |
| print("=" * 60) |
| print("Google Drive OAuth Token Generator") |
| print("=" * 60) |
| print() |
| |
| |
| client_id = input("Enter GOOGLE_OAUTH_CLIENT_ID: ").strip() |
| client_secret = input("Enter GOOGLE_OAUTH_CLIENT_SECRET: ").strip() |
| |
| if not client_id or not client_secret: |
| print("β Client ID and Secret are required!") |
| return |
| |
| |
| client_config = { |
| "installed": { |
| "client_id": client_id, |
| "client_secret": client_secret, |
| "auth_uri": "https://accounts.google.com/o/oauth2/auth", |
| "token_uri": "https://oauth2.googleapis.com/token", |
| "redirect_uris": ["urn:ietf:wg:oauth:2.0:oob", "http://localhost"] |
| } |
| } |
| |
| print() |
| print("Starting OAuth flow...") |
| print("A browser window will open. Please authorize the application.") |
| print() |
| |
| try: |
| |
| flow = InstalledAppFlow.from_client_config( |
| client_config, |
| SCOPES, |
| redirect_uri='http://localhost:8080' |
| ) |
| |
| credentials = flow.run_local_server(port=8080) |
| |
| print() |
| print("=" * 60) |
| print("β
SUCCESS! Copy these values to HF Space secrets:") |
| print("=" * 60) |
| print() |
| print(f"GOOGLE_OAUTH_CLIENT_ID={client_id}") |
| print(f"GOOGLE_OAUTH_CLIENT_SECRET={client_secret}") |
| print(f"GOOGLE_DRIVE_REFRESH_TOKEN={credentials.refresh_token}") |
| print() |
| print("β οΈ IMPORTANT: Keep the REFRESH_TOKEN secret!") |
| print(" Add it to: HF Space β Settings β Repository secrets") |
| print() |
| |
| |
| with open('.env.local', 'w') as f: |
| f.write(f"GOOGLE_OAUTH_CLIENT_ID={client_id}\n") |
| f.write(f"GOOGLE_OAUTH_CLIENT_SECRET={client_secret}\n") |
| f.write(f"GOOGLE_DRIVE_REFRESH_TOKEN={credentials.refresh_token}\n") |
| print("π Also saved to .env.local file") |
| |
| except Exception as e: |
| print(f"\nβ Error: {e}") |
| print("\nMake sure you have the google-auth-oauthlib package installed:") |
| print(" pip install google-auth-oauthlib") |
|
|
| if __name__ == "__main__": |
| main() |
|
|