
    ;$                     .   S r SSKJr  SSKrSSKrSSKrSSKJr  SSKJr  SSKJ	r	  SSKJ
r
  SSKJr  SS	KJr  / S
QrSrSrSrSrS r\" 5       r " S S\R*                  5      r " S S\R.                  5      r\R2                  " S5      SS j5       rS rg)zCommand-line tools for authenticating via OAuth 2.0

Do the OAuth 2.0 Web Server dance for a command line application. Stores the
generated credentials in a common file that is used by other example apps in
the same directory.
    )print_functionN)BaseHTTPServer)http_client)input)urllib)_helpers)client)	argparserrun_flowmessage_if_missingzWARNING: Please configure OAuth 2.0

To make this sample run you will need to populate the client_secrets.json file
found at:

   {file_path}

with information from the APIs Console <https://code.google.com/apis/console>.

a  
Failed to start a local webserver listening on either port 8080
or port 8090. Please check your firewall settings and locally
running programs that may be blocking or using those ports.

Falling back to --noauth_local_webserver and continuing with
authorization.
z
Your browser has been opened to visit:

    {address}

If your browser is on a different machine then exit and re-run this
application with the command-line parameter

  --noauth_local_webserver
z:
Go to the following link in your browser:

    {address}
c                      SS K n U R                  SS9nUR                  SSSS9  UR                  SS	SS
S9  UR                  SSSS9  UR                  SSS/[        SSS9  UR                  SS/ SQSS9  U$ ! [         a     g f = f)Nr   F)add_helpz--auth_host_name	localhostz)Hostname when running a local web server.)defaulthelpz--noauth_local_webserver
store_truezDo not run a local web server.)actionr   r   z--noopen_browserz(Do not attempt to open a browser window.z--auth_host_porti  i  *z!Port web server should listen on.)r   typenargsr   z--logging_levelERROR)DEBUGINFOWARNINGr   CRITICALz Set the logging level of detail.)r   choicesr   )argparseImportErrorArgumentParseradd_argumentint)r   parsers     1platform/bq/third_party/oauth2client_4_0/tools.py_CreateArgumentParserr$   N   s     $$e$4F
*KH  J
2< %,L  N
* %G  I *T4Ls!(K  M
7A/  1 M!  s   A8 8
BBc                       \ rS rSrSr0 rSrg)ClientRedirectServerj   zA server to handle OAuth 2.0 redirects back to localhost.

Waits for a single request and parses the query parameters
into query_params and then stops serving.
 N)__name__
__module____qualname____firstlineno____doc__query_params__static_attributes__r(       r#   r&   r&   j   s    
 Lr0   r&   c                   $    \ rS rSrSrS rS rSrg)ClientRedirectHandlers   zA handler for OAuth 2.0 redirects back to localhost.

Waits for a single request and parses the query parameters
into the servers query_params and then stops serving.
c                    U R                  [        R                  5        U R                  SS5        U R	                  5         [
        R                  R                  U R                  5      n[        R                  " UR                  5      nX R                  l        U R                  R                  S5        U R                  R                  S5        U R                  R                  S5        g)zHandle a GET request.

Parses the query parameters and prints a message
if the flow has completed. Note that we can't detect
if an error occurred.
zContent-typez	text/htmls7   <html><head><title>Authentication Status</title></head>s3   <body><p>The authentication flow has completed.</p>s   </body></html>N)send_responser   OKsend_headerend_headersr   parseurlparsepathr   parse_unique_urlencodedqueryserverr.   wfilewrite)selfpartsr=   s      r#   do_GETClientRedirectHandler.do_GETz   s     	;>>*5%%dii000=#( 

F	H

B	D

*+r0   c                     g)zADo not log messages to stdout while running as cmd. line program.Nr(   )rA   formatargss      r#   log_message!ClientRedirectHandler.log_message   s    r0   r(   N)r)   r*   r+   r,   r-   rC   rH   r/   r(   r0   r#   r2   r2   s   s    ,&Pr0   r2      c                    Uc  [         R                  5       n[        R                  " 5       R	                  [        [        UR                  5      5        UR                  (       dZ  SnSnUR                   H$  nUn [        UR                  U4[        5      nSn  O   U(       + Ul        U(       d  [        [        5        UR                  (       d  SR!                  UR                  WS9nO["        R$                  nXl        U R)                  5       n	UR                  (       d  UR*                  (       a  [        [,        R!                  U	S95        O1SSKn
U
R1                  U	SSS	9  [        [2        R!                  U	S95        SnUR                  (       dx  WR5                  5         S
UR6                  ;   a  [8        R:                  " S5        SUR6                  ;   a  UR6                  S   nO;[        S5        [8        R:                  " S5        O[=        S5      R?                  5       n U RA                  XS9nURE                  W5        URG                  U5        [        S5        U$ ! [        R                   a     GM  f = f! ["        RB                   a/  n[8        R:                  " SR!                  U5      5         SnANSnAff = f)a-  Core code for a command-line application.

The ``run()`` function is called from your application and runs
through all the steps to obtain credentials. It takes a ``Flow``
argument and attempts to open an authorization server page in the
user's default web browser. The server asks the user to grant your
application access to the user's data. If the user grants access,
the ``run()`` function returns new credentials. The new credentials
are also stored in the ``storage`` argument, which updates the file
associated with the ``Storage`` object.

It presumes it is run from a command-line application and supports the
following flags:

    ``--auth_host_name`` (string, default: ``localhost``)
       Host name to use when running a local web server to handle
       redirects during OAuth authorization.

    ``--auth_host_port`` (integer, default: ``[8080, 8090]``)
       Port to use when running a local web server to handle redirects
       during OAuth authorization. Repeat this option to specify a list
       of values.

    ``--[no]auth_local_webserver`` (boolean, default: ``True``)
       Run a local web server to handle redirects during OAuth
       authorization.

The tools module defines an ``ArgumentParser`` the already contains the
flag definitions that ``run()`` requires. You can pass that
``ArgumentParser`` to your ``ArgumentParser`` constructor::

    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter,
        parents=[tools.argparser])
    flags = parser.parse_args(argv)

Args:
    flow: Flow, an OAuth 2.0 Flow to step through.
    storage: Storage, a ``Storage`` to store the credential in.
    flags: ``argparse.Namespace``, (Optional) The command-line flags. This
           is the object returned from calling ``parse_args()`` on
           ``argparse.ArgumentParser`` as described above. Defaults
           to ``argparser.parse_args()``.
    http: An instance of ``httplib2.Http.request`` or something that
          acts like it.

Returns:
    Credentials, the obtained credential.
NFr   Tzhttp://{host}:{port}/)hostport)address   )new	autoraiseerrorz$Authentication request was rejected.codez>Failed to find "code" in the query parameters of the redirect.z*Try running with --noauth_local_webserver.zEnter verification code: )httpzAuthentication has failed: {0}zAuthentication successful.)$r
   
parse_argslogging	getLoggersetLevelgetattrlogging_levelnoauth_local_webserverauth_host_portr&   auth_host_namer2   socketrR   print_FAILED_START_MESSAGErF   r	   OOB_CALLBACK_URNredirect_uristep1_get_authorize_urlnoopen_browser_GO_TO_LINK_MESSAGE
webbrowseropen_BROWSER_OPENED_MESSAGEhandle_requestr.   sysexitr   stripstep2_exchangeFlowExchangeErrorput	set_store)flowstorageflagsrT   successport_numberrM   httpdoauth_callbackauthorize_urlrf   rS   
credentiales                 r#   r   r      sT   h }$$&  %2E2E!FG''((DK,e.B.BD-I-BD
  ) ,3{$'(''077%%K 8 9  00&002M##u';';!(((?@1=%,,],CDD''e(((HH;<U'''%%f-D % &HHAB01779=(((9
 KK
!	
&'_ << P ## =188;<<=s*   ;I$&J  $I=<I= K%J>>Kc                 (    [         R                  U S9$ )zAHelpful message to display if the CLIENT_SECRETS file is missing.)	file_path)_CLIENT_SECRETS_MESSAGErF   )filenames    r#   r   r     s    "))H)==r0   )NN)r-   
__future__r   rV   r^   rj   	six.movesr   r   r   r   oauth2client_4_0r   r	   __all__r}   r`   rh   re   r$   r
   
HTTPServerr&   BaseHTTPRequestHandlerr2   
positionalr   r   r(   r0   r#   <module>r      s     &   
 $ !   % # :	  	  2 "#	>44 PNAA P< 
Qm m`>r0   