我们从Python开源项目中,提取了以下30个代码示例,用于说明如何使用oauth2client.client.SERVICE_ACCOUNT。
def from_json_keyfile_name(cls, filename, scopes=''): """Factory constructor from JSON keyfile by name. Args: filename: string, The location of the keyfile. scopes: List or string, (Optional) Scopes to use when acquiring an access token. Returns: ServiceAccountCredentials, a credentials object created from the keyfile. Raises: ValueError, if the credential type is not :data:`SERVICE_ACCOUNT`. KeyError, if one of the expected keys is not present in the keyfile. """ with open(filename, 'r') as file_obj: client_credentials = json.load(file_obj) return cls._from_parsed_json_keyfile(client_credentials, scopes)
def from_json_keyfile_dict(cls, keyfile_dict, scopes=''): """Factory constructor from parsed JSON keyfile. Args: keyfile_dict: dict-like object, The parsed dictionary-like object containing the contents of the JSON keyfile. scopes: List or string, (Optional) Scopes to use when acquiring an access token. Returns: ServiceAccountCredentials, a credentials object created from the keyfile. Raises: ValueError, if the credential type is not :data:`SERVICE_ACCOUNT`. KeyError, if one of the expected keys is not present in the keyfile. """ return cls._from_parsed_json_keyfile(keyfile_dict, scopes)
def _from_parsed_json_keyfile(cls, keyfile_dict, scopes): """Helper for factory constructors from JSON keyfile. Args: keyfile_dict: dict-like object, The parsed dictionary-like object containing the contents of the JSON keyfile. scopes: List or string, Scopes to use when acquiring an access token. Returns: ServiceAccountCredentials, a credentials object created from the keyfile contents. Raises: ValueError, if the credential type is not :data:`SERVICE_ACCOUNT`. KeyError, if one of the expected keys is not present in the keyfile. """ creds_type = keyfile_dict.get('type') if creds_type != SERVICE_ACCOUNT: raise ValueError('Unexpected credentials type', creds_type, 'Expected', SERVICE_ACCOUNT) service_account_email = keyfile_dict['client_email'] private_key_pkcs8_pem = keyfile_dict['private_key'] private_key_id = keyfile_dict['private_key_id'] client_id = keyfile_dict['client_id'] signer = crypt.Signer.from_string(private_key_pkcs8_pem) credentials = cls(service_account_email, signer, scopes=scopes, private_key_id=private_key_id, client_id=client_id) credentials._private_key_pkcs8_pem = private_key_pkcs8_pem return credentials
def from_json_keyfile_name(cls, filename, scopes='', token_uri=None, revoke_uri=None): """Factory constructor from JSON keyfile by name. Args: filename: string, The location of the keyfile. scopes: List or string, (Optional) Scopes to use when acquiring an access token. token_uri: string, URI for OAuth 2.0 provider token endpoint. If unset and not present in the key file, defaults to Google's endpoints. revoke_uri: string, URI for OAuth 2.0 provider revoke endpoint. If unset and not present in the key file, defaults to Google's endpoints. Returns: ServiceAccountCredentials, a credentials object created from the keyfile. Raises: ValueError, if the credential type is not :data:`SERVICE_ACCOUNT`. KeyError, if one of the expected keys is not present in the keyfile. """ with open(filename, 'r') as file_obj: client_credentials = json.load(file_obj) return cls._from_parsed_json_keyfile(client_credentials, scopes, token_uri=token_uri, revoke_uri=revoke_uri)
def from_json_keyfile_dict(cls, keyfile_dict, scopes='', token_uri=None, revoke_uri=None): """Factory constructor from parsed JSON keyfile. Args: keyfile_dict: dict-like object, The parsed dictionary-like object containing the contents of the JSON keyfile. scopes: List or string, (Optional) Scopes to use when acquiring an access token. token_uri: string, URI for OAuth 2.0 provider token endpoint. If unset and not present in keyfile_dict, defaults to Google's endpoints. revoke_uri: string, URI for OAuth 2.0 provider revoke endpoint. If unset and not present in keyfile_dict, defaults to Google's endpoints. Returns: ServiceAccountCredentials, a credentials object created from the keyfile. Raises: ValueError, if the credential type is not :data:`SERVICE_ACCOUNT`. KeyError, if one of the expected keys is not present in the keyfile. """ return cls._from_parsed_json_keyfile(keyfile_dict, scopes, token_uri=token_uri, revoke_uri=revoke_uri)
def test_get_application_default_credential_from_malformed_file_1(self): credentials_file = datafile( os.path.join('gcloud', 'application_default_credentials_malformed_1.json')) expected_err_msg = ( "'type' field should be defined \(and have one of the '{0}' or " "'{1}' values\)".format(client.AUTHORIZED_USER, client.SERVICE_ACCOUNT)) with self.assertRaisesRegexp(client.ApplicationDefaultCredentialsError, expected_err_msg): client._get_application_default_credential_from_file( credentials_file)
def test_from_stream_malformed_file_1(self): credentials_file = datafile( os.path.join('gcloud', 'application_default_credentials_malformed_1.json')) expected_err_msg = ( 'An error was encountered while reading json file: ' + credentials_file + ' \(provided as parameter to the from_stream\(\) method\): ' + "'type' field should be defined \(and have one of the '" + client.AUTHORIZED_USER + "' or '" + client.SERVICE_ACCOUNT + "' values\)") with self.assertRaisesRegexp(client.ApplicationDefaultCredentialsError, expected_err_msg): self.get_a_google_credentials_object().from_stream( credentials_file)
def test_from_json_keyfile_name_factory_bad_type(self): type_ = 'bad-type' self.assertNotEqual(type_, client.SERVICE_ACCOUNT) payload = {'type': type_} with self.assertRaises(ValueError): self._from_json_keyfile_name_helper(payload)
def test_from_json_keyfile_name_factory_missing_field(self): payload = { 'type': client.SERVICE_ACCOUNT, 'client_id': 'my-client', } with self.assertRaises(KeyError): self._from_json_keyfile_name_helper(payload)
def _from_parsed_json_keyfile(cls, keyfile_dict, scopes, token_uri=None, revoke_uri=None): """Helper for factory constructors from JSON keyfile. Args: keyfile_dict: dict-like object, The parsed dictionary-like object containing the contents of the JSON keyfile. scopes: List or string, Scopes to use when acquiring an access token. token_uri: string, URI for OAuth 2.0 provider token endpoint. If unset and not present in keyfile_dict, defaults to Google's endpoints. revoke_uri: string, URI for OAuth 2.0 provider revoke endpoint. If unset and not present in keyfile_dict, defaults to Google's endpoints. Returns: ServiceAccountCredentials, a credentials object created from the keyfile contents. Raises: ValueError, if the credential type is not :data:`SERVICE_ACCOUNT`. KeyError, if one of the expected keys is not present in the keyfile. """ creds_type = keyfile_dict.get('type') if creds_type != client.SERVICE_ACCOUNT: raise ValueError('Unexpected credentials type', creds_type, 'Expected', client.SERVICE_ACCOUNT) service_account_email = keyfile_dict['client_email'] private_key_pkcs8_pem = keyfile_dict['private_key'] private_key_id = keyfile_dict['private_key_id'] client_id = keyfile_dict['client_id'] if not token_uri: token_uri = keyfile_dict.get('token_uri', oauth2client.GOOGLE_TOKEN_URI) if not revoke_uri: revoke_uri = keyfile_dict.get('revoke_uri', oauth2client.GOOGLE_REVOKE_URI) signer = crypt.Signer.from_string(private_key_pkcs8_pem) credentials = cls(service_account_email, signer, scopes=scopes, private_key_id=private_key_id, client_id=client_id, token_uri=token_uri, revoke_uri=revoke_uri) credentials._private_key_pkcs8_pem = private_key_pkcs8_pem return credentials
def _from_parsed_json_keyfile(cls, keyfile_dict, scopes, token_uri=None, revoke_uri=None): """Helper for factory constructors from JSON keyfile. Args: keyfile_dict: dict-like object, The parsed dictionary-like object containing the contents of the JSON keyfile. scopes: List or string, Scopes to use when acquiring an access token. token_uri: string, URI for OAuth 2.0 provider token endpoint. If unset and not present in keyfile_dict, defaults to Google's endpoints. revoke_uri: string, URI for OAuth 2.0 provider revoke endpoint. If unset and not present in keyfile_dict, defaults to Google's endpoints. Returns: ServiceAccountCredentials, a credentials object created from the keyfile contents. Raises: ValueError, if the credential type is not :data:`SERVICE_ACCOUNT`. KeyError, if one of the expected keys is not present in the keyfile. """ creds_type = keyfile_dict.get('type') if creds_type != SERVICE_ACCOUNT: raise ValueError('Unexpected credentials type', creds_type, 'Expected', SERVICE_ACCOUNT) service_account_email = keyfile_dict['client_email'] private_key_pkcs8_pem = keyfile_dict['private_key'] private_key_id = keyfile_dict['private_key_id'] client_id = keyfile_dict['client_id'] if not token_uri: token_uri = keyfile_dict.get('token_uri', GOOGLE_TOKEN_URI) if not revoke_uri: revoke_uri = keyfile_dict.get('revoke_uri', GOOGLE_REVOKE_URI) signer = crypt.Signer.from_string(private_key_pkcs8_pem) credentials = cls(service_account_email, signer, scopes=scopes, private_key_id=private_key_id, client_id=client_id, token_uri=token_uri, revoke_uri=revoke_uri) credentials._private_key_pkcs8_pem = private_key_pkcs8_pem return credentials