private async Task <RepositoryEnrichedTokenSet> RenewToken(string athleteId, string refreshToken, string existingAccessToken) { TokenExchangePayload payload = new TokenExchangePayload { client_id = _clientId, client_secret = _clientSecret, refresh_token = refreshToken, grant_type = "refresh_token" }; StravaTokenExchangeResponse stravaTokenResponse = await _tokenEndpoint .PostJsonAsync(payload) .ReceiveJson <StravaTokenExchangeResponse>(); await SaveTokens(athleteId, stravaTokenResponse.access_token, stravaTokenResponse.refresh_token, ExpiresAtUtc(stravaTokenResponse)); await DeleteOldAccessToken(existingAccessToken); return(new RepositoryEnrichedTokenSet { AccessToken = stravaTokenResponse.access_token, ExpiresAtUtc = ExpiresAtUtc(stravaTokenResponse), AthleteId = athleteId, RefreshToken = stravaTokenResponse.refresh_token }); }
public async Task <TokenExchangeResponse> TokenExchange(string code) { TokenExchangePayload payload = new TokenExchangePayload { client_id = _clientId, client_secret = _clientSecret, code = code, grant_type = "authorization_code" }; StravaTokenExchangeResponse stravaTokenResponse = await _tokenEndpoint .PostJsonAsync(payload) .ReceiveJson <StravaTokenExchangeResponse>(); await SaveTokens(stravaTokenResponse.athlete.id, stravaTokenResponse.access_token, stravaTokenResponse.refresh_token, ExpiresAtUtc(stravaTokenResponse)); return(new TokenExchangeResponse { TokenSet = new TokenSet { AthleteId = stravaTokenResponse.athlete.id, AccessToken = stravaTokenResponse.access_token, ExpiresAtUtc = ExpiresAtUtc(stravaTokenResponse) }, AthleteSummary = new AthleteSummary { EmailAddress = stravaTokenResponse.athlete?.email, FirstName = stravaTokenResponse.athlete?.firstname, Id = stravaTokenResponse.athlete?.id, LastName = stravaTokenResponse.athlete?.lastname, MeasurementPreference = stravaTokenResponse.athlete?.measurement_preference, ProfileImageUrl = stravaTokenResponse.athlete?.profile } }); }
private static DateTime ExpiresAtUtc(StravaTokenExchangeResponse stravaTokenResponse) { // strava returns this in seconds rather than the milliseconds in JavaScript's getTime() return(new DateTime(1970, 01, 01).AddSeconds(stravaTokenResponse.expires_at)); }