o
    yZhh                     @   s   d Z ddlZzddlZddlZdZW n ey   dZY nw zddlZW n ey/   dZY nw zddlZddl	ZdZ
W n eyG   dZ
Y nw dddZdddZd	d
 Zdd Zdd Zdd Zdd Zdd ZdS )z=Helpers for authentication using oauth2client or google-auth.    NTFc                 C   s&   t rtjj| ||d\}}|S td)z'Returns credentials loaded from a file.scopesquota_project_idzAclient_options.credentials_file is only supported in google-auth.)HAS_GOOGLE_AUTHgoogleauthZload_credentials_from_fileEnvironmentError)filenamer   r   credentials_ r   L/var/www/html/lang_env/lib/python3.10/site-packages/googleapiclient/_auth.pycredentials_from_file)   s   
r   c                 C   sL   t rtjj| |d\}}|S tr"| dus|durtdtjj	 S td)z(Returns Application Default Credentials.r   Nzwclient_options.scopes and client_options.quota_project_id are not supported in oauth2client.Please install google-auth.zZNo authentication library is available. Please install either google-auth or oauth2client.)
r   r   r   defaultHAS_OAUTH2CLIENTr   oauth2clientclientZGoogleCredentialsZget_application_default)r   r   r
   r   r   r   r   default_credentials6   s   
r   c                 C   sV   t rt| tjjjrtjj| |S z|  r| |W S | W S  t	y*   |  Y S w )a  Scopes the credentials if necessary.

    Args:
        credentials (Union[
            google.auth.credentials.Credentials,
            oauth2client.client.Credentials]): The credentials to scope.
        scopes (Sequence[str]): The list of scopes.

    Returns:
        Union[google.auth.credentials.Credentials,
            oauth2client.client.Credentials]: The scoped credentials.
    )
r   
isinstancer   r   r
   CredentialsZwith_scopes_if_requiredZcreate_scoped_requiredZcreate_scopedAttributeError)r
   r   r   r   r   with_scopesK   s   r   c                 C   sL   ddl m} tr t| tjjjr tdu rt	dtj
| | dS | | S )aa  Returns an http client that is authorized with the given credentials.

    Args:
        credentials (Union[
            google.auth.credentials.Credentials,
            oauth2client.client.Credentials]): The credentials to use.

    Returns:
        Union[httplib2.Http, google_auth_httplib2.AuthorizedHttp]: An
            authorized http client.
    r   )
build_httpNzCredentials from google.auth specified, but google-api-python-client is unable to use these credentials unless google-auth-httplib2 is installed. Please install google-auth-httplib2.http)Zgoogleapiclient.httpr   r   r   r   r   r
   r   google_auth_httplib2
ValueErrorZAuthorizedHttpZ	authorize)r
   r   r   r   r   authorized_httpd   s   r   c                 C   s:   t  }trt| tjjjrt	|}| 
|S | 
|S N)httplib2ZHttpr   r   r   r   r
   r   r   Requestrefresh)r
   Zrefresh_httprequestr   r   r   refresh_credentials   s
   


r#   c                 C   s   t | st|  | |S r   )is_validr#   apply)r
   headersr   r   r   apply_credentials   s   
r'   c                 C   s,   t rt| tjjjr| jS | jd uo| j S r   )	r   r   r   r   r
   r   ZvalidZaccess_tokenZaccess_token_expired)r
   r   r   r   r$      s
   
r$   c                 C   sB   | d u rd S t | jdr| jjS t | drt| jtjs| jS d S )Nr
   )hasattrr"   r
   r   r   r   r   r   r   r   get_credentials_from_http   s   r)   )NN)__doc__r   Zgoogle.authr   Zgoogle.auth.credentialsr   ImportErrorr   r   Zoauth2client.clientr   r   r   r   r   r#   r'   r$   r)   r   r   r   r   <module>   s:   


