Unofficial dotnet garmin connect client
This package allows you to request your device, activity and health data from your Garmin Connect account.
WARNING! Use the library only for personal automation without too many accounts. For other needs request access to the developer program.
dotnet add package Unofficial.Garmin.Connectvar authParameters = new BasicAuthParameters("<garmin login>", "<garmin password>");
var client = new GarminConnectClient(new GarminConnectContext(new HttpClient(), authParameters));var authParameters = new BasicAuthParameters("<garmin login>", "<garmin password>");
var mfaCode = new StaticMfaCode();
var client = new GarminConnectClient(new GarminConnectContext(new HttpClient(), authParameters, mfaCode));Example IMfaCodeProvider implementation:
public class StaticMfaCode : IMfaCodeProvider
{
public Task<string> GetMfaCodeAsync()
{
// 1. static code
var code = "123456";
// 2. wait for input from console
// var code = Console.ReadLine().Trim();
// 3. any other approach: read from file, env var, form input, etc.
return Task.FromResult(code);
}
}To avoid re-authenticating on every startup, pass an ITokenCache implementation. The OAuth2 token is reused until it expires.
In-memory cache (default, cleared on restart):
// InMemoryTokenCache is used automatically when no cache is specified
var client = new GarminConnectClient(new GarminConnectContext(new HttpClient(), authParameters, mfaCode));File cache (persists across restarts):
var tokenCache = new FileTokenCache("/path/to/token.json");
var client = new GarminConnectClient(
new GarminConnectContext(new HttpClient(), authParameters, mfaCode, tokenCache));Custom cache (Redis, database, etc.):
Implement ITokenCache to plug in any storage backend:
public class RedisTokenCache : ITokenCache
{
public async Task<OAuth2Token> GetOAuth2Token(CancellationToken cancellationToken)
{
// read and return token from Redis, or null if missing/expired
}
public async Task SetOAuth2Token(OAuth2Token token, CancellationToken cancellationToken)
{
// write token to Redis with TTL of token.ExpiresIn seconds
}
}- build
./pack.sh - pack
./publish.sh
- set environment variables
GARMIN_LOGINandGARMIN_PASSWORD- JetBrains Rider:
File | Settings | Build, Execution, Deployment | Unit Testing | Test Runner
- JetBrains Rider:
- The first vision from cyberjunky/python-garminconnect
- Icons made by Freepik from www.flaticon.com
- Converter JSON to C# app.quicktype.io
- and Garmin for the best devices