1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15  """Helpers for authentication using oauth2client or google-auth.""" 
 16   
 17  import httplib2 
 18   
 19  try: 
 20      import google.auth 
 21      import google.auth.credentials 
 22   
 23      HAS_GOOGLE_AUTH = True 
 24  except ImportError:   
 25      HAS_GOOGLE_AUTH = False 
 26   
 27  try: 
 28      import google_auth_httplib2 
 29  except ImportError:   
 30      google_auth_httplib2 = None 
 31   
 32  try: 
 33      import oauth2client 
 34      import oauth2client.client 
 35   
 36      HAS_OAUTH2CLIENT = True 
 37  except ImportError:   
 38      HAS_OAUTH2CLIENT = False 
 39   
 40   
 42      """Returns credentials loaded from a file.""" 
 43      if HAS_GOOGLE_AUTH: 
 44          credentials, _ = google.auth.load_credentials_from_file(filename, scopes=scopes, quota_project_id=quota_project_id) 
 45          return credentials 
 46      else: 
 47          raise EnvironmentError( 
 48          "client_options.credentials_file is only supported in google-auth.") 
  49   
 50   
 52      """Returns Application Default Credentials.""" 
 53      if HAS_GOOGLE_AUTH: 
 54          credentials, _ = google.auth.default(scopes=scopes, quota_project_id=quota_project_id) 
 55          return credentials 
 56      elif HAS_OAUTH2CLIENT: 
 57          if scopes is not None or quota_project_id is not None: 
 58              raise EnvironmentError( 
 59                  "client_options.scopes and client_options.quota_project_id are not supported in oauth2client." 
 60                  "Please install google-auth." 
 61              ) 
 62          return oauth2client.client.GoogleCredentials.get_application_default() 
 63      else: 
 64          raise EnvironmentError( 
 65              "No authentication library is available. Please install either " 
 66              "google-auth or oauth2client." 
 67          ) 
  68   
 69   
 71      """Scopes the credentials if necessary. 
 72   
 73      Args: 
 74          credentials (Union[ 
 75              google.auth.credentials.Credentials, 
 76              oauth2client.client.Credentials]): The credentials to scope. 
 77          scopes (Sequence[str]): The list of scopes. 
 78   
 79      Returns: 
 80          Union[google.auth.credentials.Credentials, 
 81              oauth2client.client.Credentials]: The scoped credentials. 
 82      """ 
 83      if HAS_GOOGLE_AUTH and isinstance(credentials, google.auth.credentials.Credentials): 
 84          return google.auth.credentials.with_scopes_if_required(credentials, scopes) 
 85      else: 
 86          try: 
 87              if credentials.create_scoped_required(): 
 88                  return credentials.create_scoped(scopes) 
 89              else: 
 90                  return credentials 
 91          except AttributeError: 
 92              return credentials 
  93   
 94   
 96      """Returns an http client that is authorized with the given credentials. 
 97   
 98      Args: 
 99          credentials (Union[ 
100              google.auth.credentials.Credentials, 
101              oauth2client.client.Credentials]): The credentials to use. 
102   
103      Returns: 
104          Union[httplib2.Http, google_auth_httplib2.AuthorizedHttp]: An 
105              authorized http client. 
106      """ 
107      from googleapiclient.http import build_http 
108   
109      if HAS_GOOGLE_AUTH and isinstance(credentials, google.auth.credentials.Credentials): 
110          if google_auth_httplib2 is None: 
111              raise ValueError( 
112                  "Credentials from google.auth specified, but " 
113                  "google-api-python-client is unable to use these credentials " 
114                  "unless google-auth-httplib2 is installed. Please install " 
115                  "google-auth-httplib2." 
116              ) 
117          return google_auth_httplib2.AuthorizedHttp(credentials, http=build_http()) 
118      else: 
119          return credentials.authorize(build_http()) 
 120   
121   
123       
124       
125       
126       
127      refresh_http = httplib2.Http() 
128      if HAS_GOOGLE_AUTH and isinstance(credentials, google.auth.credentials.Credentials): 
129          request = google_auth_httplib2.Request(refresh_http) 
130          return credentials.refresh(request) 
131      else: 
132          return credentials.refresh(refresh_http) 
 133   
134   
140   
141   
143      if HAS_GOOGLE_AUTH and isinstance(credentials, google.auth.credentials.Credentials): 
144          return credentials.valid 
145      else: 
146          return ( 
147              credentials.access_token is not None 
148              and not credentials.access_token_expired 
149          ) 
 150   
151   
153      if http is None: 
154          return None 
155      elif hasattr(http.request, "credentials"): 
156          return http.request.credentials 
157      elif hasattr(http, "credentials") and not isinstance( 
158          http.credentials, httplib2.Credentials 
159      ): 
160          return http.credentials 
161      else: 
162          return None 
 163