Dart
Dart
Configure Google Service Account Authentication for REST
See more REST Examples
Demonstrates Rest.SetAuthGoogle, which associates an AuthGoogle provider so Chilkat supplies a Google OAuth 2.0 bearer token on each request. The provider is configured with a service account JSON key and the required OAuth scope.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. The AuthGoogle provider performs a service-account (server-to-server) OAuth flow. For interactive, browser-based user consent, the OAuth2 provider is used instead.
Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
final rest = CkRest();
final bTls = true;
final bAutoReconnect = true;
try {
rest.connect('example.com', 443, bTls, bAutoReconnect);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Configure Google service-account authentication. Load the service account JSON key and set the
// required OAuth scope(s).
final gAuth = CkAuthGoogle();
final sbJsonKey = CkStringBuilder();
try {
sbJsonKey.loadFile('qa_data/service_account.json', 'utf-8');
} on ChilkatException {
print('Failed to load the service account JSON key.');
return;
}
final String jsonKey;
try {
jsonKey = sbJsonKey.getAsString();
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
gAuth.jsonKey = jsonKey;
// A space-delimited list of the requested permissions.
gAuth.scope = 'https://www.googleapis.com/auth/cloud-platform';
// Associate the provider so Chilkat supplies a Google bearer token on each request.
try {
rest.setAuthGoogle(gAuth);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final String responseText;
try {
responseText = rest.fullRequestNoBody('GET', '/storage/v1/b?project=my-project');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print(responseText);
}