Skip to main content

garth 0.1.0 copy "garth: ^0.1.0" to clipboard
garth: ^0.1.0 copied to clipboard

A Dart port of Garth, a Garmin SSO auth + Connect client.

example/main.dart

import 'dart:io';
import 'package:args/args.dart';
import 'package:garth/garth.dart';

void main(List<String> arguments) async {
  final parser = ArgParser()
    ..addOption('username', abbr: 'u', help: 'Garmin Connect username (email)')
    ..addOption('password', abbr: 'p', help: 'Garmin Connect password')
    ..addOption('domain',
        abbr: 'd',
        help: 'Garmin domain (e.g. garmin.com, garmin.cn)',
        defaultsTo: 'garmin.com')
    ..addFlag('help', abbr: 'h', negatable: false, help: 'Show usage');

  ArgResults results;
  try {
    results = parser.parse(arguments);
  } catch (e) {
    print(e);
    printUsage(parser);
    exit(1);
  }

  if (results['help'] as bool) {
    printUsage(parser);
    return;
  }

  final username = results['username'] as String?;
  final password = results['password'] as String?;
  final domain = results['domain'] as String;

  if (username == null || password == null) {
    print('Error: Username and password are required.');
    printUsage(parser);
    exit(1);
  }

  // Configure the domain before login
  garth.configure(domain: domain);

  print('Attempting to login to Garmin Connect ($domain) as $username...');

  try {
    await login(
      username,
      password,
      promptMfa: () async {
        stdout.write('Enter MFA Code: ');
        return stdin.readLineSync() ?? '';
      },
    );

    print('Login successful!');

    // Display some info
    if (garth.oauth2Token != null) {
      print('-------------------------------');
      print(garth.domain);
      print('-------------------------------');
      print(garth.oauth1Token!.toJson());
      print('-------------------------------');
      print(garth.oauth2Token!.toJson());
      print('-------------------------------');
    }

    // You can now use garth client to fetch data
    // Example: fetch social profile
    // final profile = await garth.connectapi('/user-profile-service/socialProfile');
    // print('Profile Name: ${profile['fullName']}');
  } catch (e) {
    print('Login failed: $e');
    exit(1);
  }
}

void printUsage(ArgParser parser) {
  print(
      'Usage: dart example/main.dart -u <username> -p <password> [-d <domain>]');
  print(parser.usage);
}
0
likes
150
points
27
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A Dart port of Garth, a Garmin SSO auth + Connect client.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

crypto, http

More

Packages that depend on garth