Skip to content
kahlinPublic

About

Garmin Connect authentication for .NET

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Garth.NET

Unofficial .NET libraries and CLI for authenticating with Garmin Connect. This is based on the Garth project for Python but is smaller in scope.

  • Authentication uses OAuth1 and OAuth2 token authentication after initial login.
  • Supports MFA.
  • Auto-refresh of OAuth2 token when expired.
  • Pluggable token storage.

This repository contains:

  • src\Garth.NET.Authentication: OAuth1/OAuth2 authentication, token exchange/refresh, and pluggable token storage.
  • src\Garth.NET.GarminConnectClient: Typed HTTP client and handlers for Garmin Connect (e.g., heart-rate).
  • src\Garth.NET.Cli: Simple CLI that uses the libraries. This is mostly a simple testbed for development and debugging.
  • src\Garth.NET.Tests: NUnit tests and utilities.

Not affiliated with or endorsed by Garmin.

Status

This is a work in progress but the authentication library is functional and complete.

  • Authentication
  • Local (pluggable) storage of tokens
  • Meaningful tests
  • Publish Nuget packages
  • Implement usage of OS credential managers or secret stores for storing tokens
  • Implement more Garmin Connect APIs (currently only heart-rate summary is supported and only as a demo)

Prerequisites

  • .NET SDK (version pinned in global.json)

Build and test

dotnet build
dotnet test

Quick start (programmatic)

Below is a minimal example showing how to wire up dependency injection, log in, and request a daily heart-rate summary.

using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Garth.NET.Authentication;
using Garth.NET.GarminConnectClient;

class Program
{
    static async Task Main()
    {
        var services = new ServiceCollection()
            .AddHttpClient()
            .AddGarminConnectAuthentication()
            .AddGarminConnectClient();

        using var sp = services.BuildServiceProvider();

        var auth = sp.GetRequiredService<IGarthAuthService>();

        string email = Environment.GetEnvironmentVariable("GARTH_EMAIL") ?? throw new InvalidOperationException("Set GARTH_EMAIL");
        string password = Environment.GetEnvironmentVariable("GARTH_PASSWORD") ?? throw new InvalidOperationException("Set GARTH_PASSWORD");

        // MFA prompt is invoked only when required by Garmin
        var tokens = await auth.LoginAsync(email, password, async () =>
        {
            Console.Write("Enter MFA code: ");
            return await Task.FromResult(Console.ReadLine()!);
        });

        var client = sp.GetRequiredService<IGarminConnectHeartRateClient>();
        var today = DateOnly.FromDateTime(DateTime.UtcNow);

        var summary = await client.GetDailyHeartRateSummaryAsync(today);
        Console.WriteLine(summary is null ? "No summary available" : $"Resting HR: {summary.RestingHeartRate}");
    }
}

Notes:

  • Token storage defaults can be configured via LocalFileTokenStorage or your own ITokenStorage implementation.
  • The HTTP pipeline uses GarthHttpHandler to attach/refresh OAuth2 tokens automatically.

CLI

A small CLI is included primarily for testing/debugging the library.

dotnet run --project src\Garth.NET.Cli -- --help

Project structure (high level)

  • Authentication
    • IGarthAuthService, GarthAuthService: login, MFA handling, token exchange and refresh
    • ConsumerKeysService: retrieves consumer keys needed for OAuth1
    • TokenStorage: ITokenStorage abstraction + LocalFileTokenStorage implementation
    • OAuth types: OAuth1Token, OAuth2Token, TokenPair
  • GarminConnectClient
    • GarminConnectClient: low-level request helper
    • GarthHttpHandler: attaches OAuth2 and refreshes when expired
    • IGarminConnectHeartRateClient: daily heart-rate summary API
  • Tests (NUnit)
    • Dependency injection wiring smoke tests
    • Token storage behavior
    • Optional login tests (require valid credentials/MFA)

Contributing

  • Open issues and PRs are welcome. Please include tests where possible and follow existing coding conventions.

About

Garmin Connect authentication for .NET

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages