Spaces:
Running
Running
| #!/usr/bin/env python3 | |
| """ | |
| Simple script to get Google Drive refresh token | |
| """ | |
| import os | |
| import json | |
| from google.oauth2.credentials import Credentials | |
| from google_auth_oauthlib.flow import InstalledAppFlow | |
| from google.auth.transport.requests import Request | |
| def get_refresh_token(): | |
| """Get refresh token for Google Drive""" | |
| print("π Getting Google Drive Refresh Token...") | |
| print("This will open your browser for authentication") | |
| print() | |
| try: | |
| SCOPES = ['https://www.googleapis.com/auth/drive.file'] | |
| creds = None | |
| # Check if token.json exists | |
| if os.path.exists('token.json'): | |
| print("π Found existing token.json") | |
| creds = Credentials.from_authorized_user_file('token.json', SCOPES) | |
| # If no valid credentials, request authorization | |
| if not creds or not creds.valid: | |
| if creds and creds.expired and creds.refresh_token: | |
| print("π Refreshing expired token...") | |
| creds.refresh(Request()) | |
| else: | |
| print("π Opening browser for authentication...") | |
| flow = InstalledAppFlow.from_client_secrets_file( | |
| 'credentials.json', SCOPES) | |
| creds = flow.run_local_server(port=8080, open_browser=True) | |
| # Save credentials for next run | |
| with open('token.json', 'w') as token: | |
| token.write(creds.to_json()) | |
| print("πΎ Token saved to token.json") | |
| # Extract refresh token | |
| refresh_token = creds.refresh_token | |
| if refresh_token: | |
| print("\nβ SUCCESS! Refresh token obtained!") | |
| print(f"π GOOGLE_REFRESH_TOKEN = {refresh_token}") | |
| # Also show client info | |
| with open('credentials.json', 'r') as f: | |
| creds_data = json.load(f) | |
| if 'web' in creds_data: | |
| client_id = creds_data['web']['client_id'] | |
| client_secret = creds_data['web']['client_secret'] | |
| print("\nπ Add these 3 secrets to your HF Space:") | |
| print(f"GOOGLE_CLIENT_ID = {client_id}") | |
| print(f"GOOGLE_CLIENT_SECRET = {client_secret}") | |
| print(f"GOOGLE_REFRESH_TOKEN = {refresh_token}") | |
| print("\nπ Next steps:") | |
| print("1. Go to: https://huggingface.co/spaces/NLong/FakeNews_Detector") | |
| print("2. Click 'Settings' tab") | |
| print("3. Add the 3 secrets above") | |
| print("4. Your app will use Google Drive for RAG storage!") | |
| return refresh_token | |
| else: | |
| print("β No refresh token found") | |
| return None | |
| except Exception as e: | |
| print(f"β Error: {e}") | |
| return None | |
| if __name__ == "__main__": | |
| get_refresh_token() | |