Spaces:
Sleeping
Sleeping
| 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() | |