ServiceAccountCredentials.fromJson constructor
Creates a new ServiceAccountCredentials from JSON.
json
can be either a Map or a JSON map encoded as a String.
The optional named argument impersonatedUser
is used to set the user
to impersonate if impersonating a user.
Implementation
factory ServiceAccountCredentials.fromJson(Object? json,
{String? impersonatedUser}) {
if (json is String) {
json = jsonDecode(json);
}
if (json is! Map) {
throw ArgumentError('json must be a Map or a String encoding a Map.');
}
final identifier = json['client_id'] as String?;
final privateKey = json['private_key'] as String?;
final email = json['client_email'] as String?;
final type = json['type'];
if (type != 'service_account') {
throw ArgumentError(
'The given credentials are not of type '
'service_account (was: $type).',
);
}
if (identifier == null || privateKey == null || email == null) {
throw ArgumentError(
'The given credentials do not contain all the '
'fields: client_id, private_key and client_email.',
);
}
final clientId = ClientId(identifier);
return ServiceAccountCredentials(
email,
clientId,
privateKey,
impersonatedUser: impersonatedUser,
);
}