File size: 2,656 Bytes
8937f53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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

# OAuth scopes needed
SCOPES = ['https://www.googleapis.com/auth/drive']

def main():
    print("=" * 60)
    print("Google Drive OAuth Token Generator")
    print("=" * 60)
    print()
    
    # Check for client credentials
    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
    
    # Create client secrets dict
    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:
        # Run OAuth flow
        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()
        
        # Save to file for convenience
        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()