def _get_application_default_credential_from_dict(client_credentials): """Build the Application Default Credentials from file.""" credentials_type = client_credentials.get('type') if credentials_type == AUTHORIZED_USER: required_fields = set(['client_id', 'client_secret', 'refresh_token']) elif credentials_type == SERVICE_ACCOUNT: required_fields = set( ['client_id', 'client_email', 'private_key_id', 'private_key']) else: raise ApplicationDefaultCredentialsError( "'type' field should be defined (and have one of the '" + AUTHORIZED_USER + "' or '" + SERVICE_ACCOUNT + "' values)") missing_fields = required_fields.difference(client_credentials.keys()) if missing_fields: _raise_exception_for_missing_fields(missing_fields) if client_credentials['type'] == AUTHORIZED_USER: return GoogleCredentials( access_token=None, client_id=client_credentials['client_id'], client_secret=client_credentials['client_secret'], refresh_token=client_credentials['refresh_token'], token_expiry=None, token_uri=GOOGLE_TOKEN_URI, user_agent='Python client library') else: # client_credentials['type'] == SERVICE_ACCOUNT from oauth2client.service_account import _JWTAccessCredentials return _JWTAccessCredentials.from_json_keyfile_dict(client_credentials)
def test_raise_exception_for_missing_fields(self): missing_fields = ["first", "second", "third"] # we can't use self.assertRaisesRegexp() because it is only in Python 2.7+ try: _raise_exception_for_missing_fields(missing_fields) self.fail("An exception was expected!") except ApplicationDefaultCredentialsError as error: self.assertEqual("The following field(s) must be defined: " + ", ".join(missing_fields), str(error))
def test_raise_exception_for_missing_fields(self): missing_fields = ['first', 'second', 'third'] # we can't use self.assertRaisesRegexp() because it is only in Python 2.7+ try: _raise_exception_for_missing_fields(missing_fields) self.fail('An exception was expected!') except ApplicationDefaultCredentialsError as error: self.assertEqual('The following field(s) must be defined: ' + ', '.join(missing_fields), str(error))
def test_raise_exception_for_missing_fields(self): missing_fields = ['first', 'second', 'third'] # we can't use self.assertRaisesRegexp() because it is only in Python 2.7+ try: _raise_exception_for_missing_fields(missing_fields) self.fail('An exception was expected!') except ApplicationDefaultCredentialsError as error: self.assertEqual('The following field(s) must be defined: ' + ', '.join(missing_fields), str(error))