o
    FѬi                     @   s   d Z ddlZddlZddlZddlmZ edZee	 j
j
Zedeed Zedeed Zd	d
gZdd Zdd Zdd Zdd Zdd ZdS )u7  
Google OAuth2 authentication handler.

Provides shared credentials for Google Drive and Gmail APIs.
Uses OAuth2 with a desktop app flow — Bill authorizes once, then
the refresh token handles everything automatically.

Setup:
1. Go to https://console.cloud.google.com
2. Create a project (or reuse one)
3. Enable: Google Drive API, Gmail API
4. Create OAuth 2.0 credentials (Desktop App type)
5. Download the JSON → save as ~/clawd/.google-credentials.json
6. Run: python setup_google_auth.py
7. Authorize in browser → token saved to ~/clawd/.google-token.json
    N)Pathzgoogle-authGOOGLE_CREDENTIALS_PATHz.google-credentials.jsonGOOGLE_TOKEN_PATHz.google-token.jsonz.https://www.googleapis.com/auth/drive.readonlyz.https://www.googleapis.com/auth/gmail.readonlyc               
   C   s  zddl m}  ddlm} W n ty   td Y dS w d}tj	t
rKz| t
t}W n tyJ } ztd|  d}W Y d}~nd}~ww |r|jr|jrz||  t| td W n ty } ztd|  d}W Y d}~nd}~ww |r|jstd	 dS |S )
zi
    Get valid Google OAuth2 credentials.
    Returns google.oauth2.credentials.Credentials or None.
    r   )Credentials)Requestzegoogle-auth not installed. Run: pip install google-auth google-auth-oauthlib google-api-python-clientNzFailed to load token: z#Google token refreshed successfullyzToken refresh failed: zLNo valid Google credentials. Run 'python setup_google_auth.py' to authorize.)google.oauth2.credentialsr   google.auth.transport.requestsr   ImportErrorloggererrorospathexists
TOKEN_PATHfrom_authorized_user_fileSCOPES	Exceptionwarningexpiredrefresh_tokenrefresh_save_tokeninfovalid)r   r   credse r   H/sessions/lucid-sleepy-lamport/mnt/clawd/whatsapp-agent/./google_auth.pyget_credentials+   sD   
r   c                 C   sz   | j | j| j| j| j| jrt| jntd}tt	d}t
j||dd W d   n1 s.w   Y  tdt	  dS )zSave credentials to token file.)tokenr   	token_uri	client_idclient_secretscopesw   )indentNzToken saved to )r   r   r    r!   r"   r#   listr   openr   jsondumpr
   r   )r   
token_datafr   r   r   r   W   s   r   c               
   C   b   t  } | sdS zddlm} |dd| dW S  ty0 } ztd|  W Y d}~dS d}~ww )z3Build and return a Google Drive API service object.Nr   builddrivev3credentialszFailed to build Drive service: r   googleapiclient.discoveryr/   r   r
   r   r   r/   r   r   r   r   build_drive_servicef      r7   c               
   C   r-   )z,Build and return a Gmail API service object.Nr   r.   gmailv1r2   zFailed to build Gmail service: r4   r6   r   r   r   build_gmail_services   r8   r;   c                   C   s   t jtot jtS )z'Check if Google credentials are set up.)r   r   r   r   CREDENTIALS_PATHr   r   r   r   is_configured   s   r=   )__doc__r   r)   loggingpathlibr   	getLoggerr
   __file__resolveparent	_BASE_DIRgetenvstrr<   r   r   r   r   r7   r;   r=   r   r   r   r   <module>   s.    


,