File size: 1,865 Bytes
2e708ff
 
 
 
 
c4fd9c8
 
 
 
 
2e708ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
from google_auth_oauthlib.flow import InstalledAppFlow

# If modifying these scopes, delete the file token.json.
# If modifying these scopes, delete the file token.json.
SCOPES = [
    'https://www.googleapis.com/auth/gmail.send',
    'https://www.googleapis.com/auth/drive.file'
]

def get_refresh_token():
    """Shows basic usage of the Gmail API.
    Lists the user's Gmail labels.
    """
    creds = None
    
    # Check if client_secret.json exists
    if not os.path.exists('client_secret.json'):
        print("Error: 'client_secret.json' not found.")
        print("Please download your OAuth 2.0 Client ID JSON file from Google Cloud Console,")
        print("rename it to 'client_secret.json', and place it in this directory.")
        return

    flow = InstalledAppFlow.from_client_secrets_file(
        'client_secret.json', SCOPES)
    
    # Run the local server flow to get credentials
    creds = flow.run_local_server(port=0)

    print("\n--- Authentication Successful ---")
    print("Here are your credentials details. Please add these to your .env file or HF Secrets:\n")
    
    # Extract client config to get ID and Secret easily if needed, though they are in the json file
    with open('client_secret.json', 'r') as f:
        client_config = json.load(f)
        installed = client_config.get('installed', client_config.get('web', {}))
        print(f"GOOGLE_CLIENT_ID={installed.get('client_id')}")
        print(f"GOOGLE_CLIENT_SECRET={installed.get('client_secret')}")

    print(f"GOOGLE_REFRESH_TOKEN={creds.refresh_token}")
    print("\n---------------------------------")
    print("Note: The access token will expire, but the refresh token is long-lived.")
    print("Use the refresh token in your application to get new access tokens automatically.")

if __name__ == '__main__':
    get_refresh_token()