Garth (Dart)
A Dart port of Garth, providing Garmin SSO authentication and a Garmin Connect client.
Features
- SSO Login: Secure login with Multi-Factor Authentication (MFA) support.
- State Persistence: Save and resume sessions easily using base64-encoded dumps.
- OAuth Exchange: Automatic exchange of OAuth1 tokens to OAuth2 tokens.
- Garmin Connect Client: Ready-to-use client for interacting with Garmin Connect APIs.
- Region Support: Supports both global (
garmin.com) and China (garmin.cn) domains.
Installation
Add garth to your pubspec.yaml:
dependencies:
garth: ^0.1.0
Then run:
dart pub get
Quick Start
import 'package:garth/garth.dart';
void main() async {
// Configure for China region if needed (defaults to 'garmin.com')
configure(domain: 'garmin.cn');
// Optional: Enable debug JSON collection to a specific folder
// enableDebug(true, 'data');
try {
// Login with email and password
await login(
'email@example.com',
'password',
promptMfa: () async {
// Handle MFA if requested by Garmin
print('Please enter MFA code:');
return '123456';
},
);
print('Login successful!');
// Fetch user profile
final profile = await UserProfile.get();
print('Display Name: ${profile.displayName}');
// Save session for later use
String dump = save();
// ... store dump somewhere ...
// Resume session later from dump
// resumeFromDump(dump);
} catch (e) {
print('Error: $e');
}
}
Command Line Example
The project includes a CLI tool in example/main.dart to test authentication.
How to Run
dart example/main.dart -u YOUR_EMAIL -p YOUR_PASSWORD [-d garmin.cn]
Options:
| Option | Abbr | Description | Default |
|---|---|---|---|
--username |
-u |
Garmin Connect username (email) | (Required) |
--password |
-p |
Garmin Connect password | (Required) |
--domain |
-d |
Garmin domain (e.g., garmin.com, garmin.cn) | garmin.com |
--help |
-h |
Show usage information | - |
Credits
- This project is a Dart port of the original Garth (Python).
License
MIT License. See LICENSE for details.