
     h                         d Z ddlZddlZddlZddlZ	 ddlmZmZmZm	Z	m
Z
 ddlmZ 	 eZdZdZd	 Zd
 Zd Zd Zd ZddZd Zd Zd ZdedefdZy# e$ r ddlmZmZmZmZ ddlm	Z	m
Z
 Y Gw xY w# e$ r Y Ow xY w)a-  
Adapted from:
http://blog.macuyiko.com/post/2016/how-to-send-html-mails-with-oauth2-and-gmail-in-python.html

1. Generate and authorize an OAuth2 (generate_oauth2_token)
2. Generate a new access tokens using a refresh token(refresh_token)
3. Generate an OAuth2 string to use for login (access_token)
    N)	urlencodequoteunquoteparse_qsurlsplit)urlopen)r   r   r   r   )r   r   zhttps://accounts.google.comzhttp://localhostc                     t         d| S )N/)GOOGLE_ACCOUNTS_BASE_URL)commands    N/var/www/api/v1/venv_getwork_v1/lib/python3.12/site-packages/yagmail/oauth2.pycommand_to_urlr      s    .88    c                     g }t        | j                         d       D ]+  }t        |d   d      }|j                  |d   d|       - dj	                  |      S )	Nc                     | d   S )Nr    )xs    r   <lambda>z#url_format_params.<locals>.<lambda>$   s    adr   )key   z~-._)safer   =&)sorteditemsr   appendjoin)paramsparam_fragmentsparamescaped_urls       r   url_format_paramsr"   "   sV    ON;E!H62%(K@A < 88O$$r   c                 h    i }| |d<   t         |d<   d|d<   d|d<   t        d      dt        |      S )	N	client_idredirect_urizhttps://mail.google.com/scopecoderesponse_typezo/oauth2/auth?)REDIRECT_URIr   r"   )r$   r   s     r   generate_permission_urlr+   *   sD    F#F;)F>0F7O$F?$_57H7PQQr   c                    i }| |d<   ||d<   ||d<   t         |d<   d|d<   t        d      }t        |      j                  d      }t	        ||      j                         j                  d      }t        j                  |      S )	Nr$   client_secretr'   r%   authorization_code
grant_typeo/oauth2/tokenUTF-8)	r*   r   r   encoder   readdecodejsonloads)r$   r-   r.   r   request_urlencoded_paramsresponses          r   call_authorize_tokensr:   3   s    F#F;+F?'F6N)F>/F< !12Kv&--g6N{N388:AA'JH::hr   c                     i }| |d<   ||d<   ||d<   d|d<   t        d      }t        |      j                  d      }t        ||      j	                         j                  d      }t        j                  |      S )Nr$   r-   refresh_tokenr/   r0   r1   )r   r   r2   r   r3   r4   r5   r6   )r$   r-   r<   r   r7   r8   r9   s          r   call_refresh_tokenr=   @   s~    F#F;+F?+F?*F< !12Kv&--g6N{N388:AA'JH::hr   c                     d| d|d}|r3t        j                  |j                  d            j                  d      }|S )Nzuser=zauth=Bearer zascii)base64	b64encoder2   r4   )usernameaccess_token	as_base64auth_strings       r   generate_oauth2_stringrF   L   s;    3;\JK&&{'9'9''BCJJ7Sr   c                     t        |       }t        d|z          t        d      }t        t	        |      j
                        d   d   }t        | ||      }|d   |d   |d   fS )Nz'Navigate to the following URL to auth:
z0Enter the localhost URL you were redirected to: r'   r   r<   rC   
expires_in)r+   printinputr   r   queryr:   )google_client_idgoogle_client_secretpermission_urlurlr.   r9   s         r   get_authorizationrP   S   sr    ,-=>N	
4~
EF
B
CC!(3-"5"56v>qA$%57KM_`HO$h~&>@VVVr   c                 0    t        | ||      }|d   |d   fS )NrC   rH   )r=   )rL   rM   google_refresh_tokenr9   s       r   refresh_authorizationrS   \   s(    !"24HJ^_HN#Xl%;;;r   c                 >    t        di |\  }}t        | |d      }|S )NT)rD   r   )rS   rF   )useroauth2_inforC   rH   rE   s        r   get_oauth_stringrW   a   s(    4C{CL*(|tLKr   oauth2_file
email_addrc                    d}t         j                  j                  |       } t         j                  j                  |       rt	        |       5 }t        j                  |      }d d d        	 d   }t        |       |t        d      }|d   }|d   }t        ||      \  }}}||j                         |j                         |j                         d}t	        | d      5 }t        j                  ||       d d d        |S t        d       t        d	       t        d
       t        |       |t        d      }t        d      }t        j                  d      }t        ||      \  }}}||j                         |j                         |j                         d}t	        | d      5 }t        j                  ||       d d d        |S # 1 sw Y   gxY w# t        $ r cY S w xY w# 1 sw Y   |S xY w# 1 sw Y   |S xY w)NzSee readme for proper setup, preventing authorization from expiring after 7 days! https://github.com/kootenpv/yagmail/blob/master/README.md#preventing-oauth-authorization-from-expiring-after-7-days	installedzYour 'email address': r$   r-   )email_addressrL   rM   rR   wzLIf you do not have an app registered for your email sending purposes, visit:z%https://console.developers.google.comzand create a new project.
zYour 'google_client_id': zYour 'google_client_secret': )ospath
expanduserisfileopenr5   loadKeyErrorrI   rJ   rP   stripdumpgetpass)	rX   rY   oauth_setup_readme_linkfrV   rL   rM   rR   _s	            r   get_oauth2_infork   g   s    f''$$[1K	ww~~k"+!))A,K 	%k2K 	%&78J&{3*?;%67GI]%^"a' 0 6 6 8$8$>$>$@$8$>$>$@	
 +s#qIIk1% $( # 	\]56+,%&78J !<=&/NO%67GI]%^"a' 0 6 6 8$8$>$>$@$8$>$>$@	
 +s#qIIk1% $M   		 $(  $s6   G+G G%&G2GG"!G"%G/2G<)F) __doc__r^   r@   r5   rg   urllib.parser   r   r   r   r   urllib.requestr   ImportErrorurlliburlparse	raw_inputrJ   	NameErrorr   r*   r   r"   r+   r:   r=   rF   rP   rS   rW   strrk   r   r   r   <module>ru      s    
   ,JJ&
	E 9 !9%R
 	 W<
* *# *m  ,99++,  		s"   A A3 A0/A03A;:A;