Skip to content

Commit 45cce3c

Browse files
committed
Add attempt to refresh token upon token validation
1 parent 842ba08 commit 45cce3c

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/Umbraco.Cms.Integrations.Crm.Hubspot.Core/Controllers/FormsController.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public async Task<ResponseDto> GetAllOAuth()
145145
Forms = ParseForms(forms).ToList()
146146
};
147147
}
148-
catch(HttpRequestException ex) when (ex.Message.Contains(HttpStatusCode.Unauthorized.ToString()))
148+
catch (HttpRequestException ex) when (ex.Message.Contains(HttpStatusCode.Unauthorized.ToString()))
149149
{
150150
_logger.LogError(string.Format(LoggingResources.OAuthFetchFormsFailed, responseContent));
151151

@@ -236,6 +236,22 @@ public async Task<ResponseDto> ValidateAccessToken()
236236
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
237237

238238
var response = await ClientFactory().SendAsync(requestMessage);
239+
if (response.StatusCode == HttpStatusCode.Unauthorized)
240+
{
241+
// Attempt to refresh the access token
242+
await _authorizationService.RefreshAccessTokenAsync();
243+
244+
_tokenService.TryGetParameters(Constants.AccessTokenDbKey, out string newAccessToken);
245+
246+
// Retry the request with the new access token
247+
requestMessage = new HttpRequestMessage
248+
{
249+
Method = HttpMethod.Get,
250+
RequestUri = new Uri(HubspotFormsApiEndpoint)
251+
};
252+
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", newAccessToken);
253+
response = await ClientFactory().SendAsync(requestMessage);
254+
}
239255

240256
return new ResponseDto
241257
{

0 commit comments

Comments
 (0)