
    FѬi                     :   S r SSKrSSKrSSKrSSKJr  \R                  " S5      r\" \5      R                  5       R                  R                  r\R                  " S\" \S-  5      5      r\R                  " S\" \S-  5      5      rS	S
/rS rS rS rS rS rg)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                      SSK Jn   SSKJn  Sn[        R                  R                  [        5      (       a   U R                  [        [        5      nU(       aY  UR                  (       aH  UR                   (       a7   UR#                  U" 5       5        [%        U5        [
        R'                  S5        U(       a  UR(                  (       d  [
        R                  S	5        gU$ ! [         a    [
        R                  S5         gf = f! [         a$  n[
        R                  SU 35        Sn SnANSnAff = f! [         a$  n[
        R                  SU 35        Sn SnANSnAff = f)
z]
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es       2/Users/bsyrros/clawd/whatsapp-agent/google_auth.pyget_credentialsr   +   s   
9: E 
ww~~j!!	99*fME 5#6#6	MM')$KK=>
 Z	
 LC  T	
   	NN3A378E	  	LL1!56E	s@   C# D =6D9 #DD
D6D11D69
E'E""E'c                    U R                   U R                  U R                  U R                  U R                  U R
                  (       a  [        U R
                  5      O[        S.n[        [        S5       n[        R                  " XSS9  SSS5        [        R                  S[         35        g! , (       d  f       N+= f)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fs      r   r   r   W   s     ,,____,,(-$u||$&J 
j#	!		** 

KK/*./ 
	s   6B11
B?c                      [        5       n U (       d  g SSKJn  U" SSU S9$ ! [         a"  n[        R                  SU 35         SnAgSnAff = f)z3Build and return a Google Drive API service object.Nr   builddrivev3credentialszFailed to build Drive service: r   googleapiclient.discoveryr1   r   r   r   r   r1   r   s      r   build_drive_servicer9   f   N    E3Wd66 6qc:;   " 
AA		Ac                      [        5       n U (       d  g SSKJn  U" SSU S9$ ! [         a"  n[        R                  SU 35         SnAgSnAff = f)z,Build and return a Gmail API service object.Nr   r0   gmailv1r4   zFailed to build Gmail service: r6   r8   s      r   build_gmail_servicer?   s   r:   r;   c                      [         R                  R                  [        5      =(       a#    [         R                  R                  [        5      $ )z'Check if Google credentials are set up.)r   r   r   r   CREDENTIALS_PATH     r   is_configuredrD      s(    77>>*%J"''..9I*JJrC   )__doc__r   r+   loggingpathlibr   	getLoggerr   __file__resolveparent	_BASE_DIRgetenvstrrA   r   r   r   r   r9   r?   rD   rB   rC   r   <module>rO      s   " 
   			=	) N""$++22	99	../  YY	(()
 54
)X0

KrC   