getClientCredentialsToken method
Get token using client credentials
Implementation
@override
Future<OAuthToken> getClientCredentialsToken({List<String>? scopes}) async {
final metadata = await _discoverMetadata();
final body = <String, String>{
'grant_type': 'client_credentials',
'client_id': config.clientId,
if (scopes != null && scopes.isNotEmpty) 'scope': scopes.join(' '),
};
final headers = <String, String>{
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
};
if (config.clientSecret != null) {
final credentials = base64Encode(
utf8.encode('${config.clientId}:${config.clientSecret}'),
);
headers['Authorization'] = 'Basic $credentials';
}
final response = await _httpClient.post(
Uri.parse(metadata.tokenEndpoint),
headers: headers,
body: body.entries
.map((e) => '${e.key}=${Uri.encodeComponent(e.value)}')
.join('&'),
);
final json = jsonDecode(response.body) as Map<String, dynamic>;
if (response.statusCode != 200) {
throw OAuthError.fromJson(json);
}
return OAuthToken.fromJson(json);
}