"<p>We apologize for the inconvenience</p>");
// Record permanent failure since this connector won't work again until
// it is reauthenticated
guestService.setApiKeyStatus(apiKey.getId(), ApiKey.Status.STATUS_PERMANENT_FAILURE, null, ApiKey.PermanentFailReason.NEEDS_REAUTH);
throw new UpdateFailedException("requires token reauthorization", true, ApiKey.PermanentFailReason.NEEDS_REAUTH);
}
// We're not on a mirrored test server. Try to swap the expired
// access token for a fresh one. Typically moves access tokens are good for
// 180 days from time of issue.
String swapTokenUrl = "https://api.moves-app.com/oauth/v1/access_token";
final String refreshToken = guestService.getApiKeyAttribute(apiKey, "refreshToken");
Map<String,String> params = new HashMap<String,String>();
params.put("refresh_token", refreshToken);
params.put("client_id", guestService.getApiKeyAttribute(apiKey, "moves.client.id"));
params.put("client_secret", guestService.getApiKeyAttribute(apiKey, "moves.client.secret"));
params.put("grant_type", "refresh_token");
String fetched;
try {
fetched = HttpUtils.fetch(swapTokenUrl, params);
// Record that this connector is now up
guestService.setApiKeyStatus(apiKey.getId(), ApiKey.Status.STATUS_UP, null, null);
} catch (Exception e) {
// Notify the user that the tokens need to be manually renewed
notificationsService.addNamedNotification(apiKey.getGuestId(), Notification.Type.WARNING, connector.statusNotificationName(),
"Heads Up. We failed in our attempt to automatically refresh your Moves authentication tokens.<br>" +
"Please head to <a href=\"javascript:App.manageConnectors()\">Manage Connectors</a>,<br>" +
"scroll to the Moves connector, delete the connector, and re-add<br>" +
"<p>We apologize for the inconvenience</p>");
// Record permanent update failure since this connector is never
// going to succeed
guestService.setApiKeyStatus(apiKey.getId(), ApiKey.Status.STATUS_PERMANENT_FAILURE, null, ApiKey.PermanentFailReason.NEEDS_REAUTH);
throw new UpdateFailedException("refresh token attempt failed", e, true, ApiKey.PermanentFailReason.NEEDS_REAUTH);
}
JSONObject token = JSONObject.fromObject(fetched);
final long expiresIn = token.getLong("expires_in");
final String access_token = token.getString("access_token");