示例#1
0
        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
            });
        }
示例#2
0
        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
                }
            });
        }