Table of Contents

Class StravaSession

Namespace
Tudormobile.Strava
Assembly
Tudormobile.Strava.dll

Strava V3 API Session

public class StravaSession
Inheritance
StravaSession
Inherited Members
Extension Methods

Remarks

Strava V3 API authentication, authorization, and access to Athletes and Activities APIs.

A StravaSession object manages the authentication and authorization of a Strava logged-in Athlete (user). Provide either an 'unauthenticated' StravaAuthorization object (ClientId and ClientSecret only) to create a new session, or you can supply the previous tokens and call one the the Refresh() methods to ensure the user is authenticated. Once authenticated, the session can be used to access the Strava API.

Constructors

StravaSession(StravaAuthorization, HttpClient?, bool)

Create and initialize a new instance.

public StravaSession(StravaAuthorization clientAuthorization, HttpClient? httpClient = null, bool useProvidedClientForRefresh = false)

Parameters

clientAuthorization StravaAuthorization

Initial authorization.

httpClient HttpClient

HttpClient to use for api requests; Optional.

useProvidedClientForRefresh bool

Indicates whether to use the provided HttpClient for token refresh operations.

Remarks

Providing an HttpClient is preferred. If provided, management of the HttpClient is the responsibility of the caller.

If the HttpClient is not provided, a single static HttpClient instance will be used with a 30-second timeout value. Be aware that this can result in issues with stale DNS and connection pooling.

Properties

Authorization

Current client authorization record.

public StravaAuthorization Authorization { get; }

Property Value

StravaAuthorization

IsAuthenticated

True if current session is authenticated, i.e., access token is provided and is not expired.

public bool IsAuthenticated { get; }

Property Value

bool

Remarks

If the current access token is within 30 seconds of expiration, the session is considered not authenticated.

Methods

RefreshAsync(HttpClient, CancellationToken)

Refresh the authorization (access and refresh) tokens using a provided HttpClient instance.

public Task<ApiResult<StravaAuthorization>> RefreshAsync(HttpClient httpClient, CancellationToken cancellationToken = default)

Parameters

httpClient HttpClient

The HttpClient to use for the request.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<ApiResult<StravaAuthorization>>

Strava API result.

Remarks

This overload allows the caller to provide a custom HttpClient instance for the token refresh request. Success or Failure is returned in the ApiResult<T> object. If successful, the session authentication is updated with the new authentication and refresh tokens, as well as the currently logged in Athlete (user) Id.

RefreshAsync(CancellationToken)

Refresh the authorization (access and refresh) tokens.

public Task<ApiResult<StravaAuthorization>> RefreshAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<ApiResult<StravaAuthorization>>

Strava API result.

Remarks

Success or Failure is returned in the ApiResult object. If successful, the session authentication is updated with the new authentication and refresh tokens, as well as the currently logged in Athlete (user) Id.

This method makes a network call to the Strava authorization endpoint to refresh the access token using a new, internal HttpClient instance. The HttpClient is disposed after the request completes. This method is thread-safe however is not expected to be called often. The access token needs to be refreshed only when expired (typically after significant timeframes, but defined by the Strava V3 API). To avoid unnecessary network calls, use the RefreshTokensAsync(CancellationToken) method which only calls this method when the current access token is expired, and call this method once per session or as needed when explicit error handling is required.

RefreshTokensAsync(CancellationToken)

Refresh the authorization tokens.

public Task<StravaSession> RefreshTokensAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<StravaSession>

Reference to the current session.

Remarks

This method silently handles authentication failures. Check IsAuthenticated after calling to verify success.

StravaApi()

Get the Strava API object.

public IStravaApi StravaApi()

Returns

IStravaApi

Basic Strava V3 API object.