private boolean isValidationNeeded(AccessToken accessToken, AccessToken previousAccessToken) {
   if (accessTokenValidation == AccessTokenValidation.ALWAYS) {
     return true;
   } else if (accessTokenValidation == AccessTokenValidation.SKIP) {
     return false;
   } else {
     return !accessToken.equals(previousAccessToken);
   }
 }
  protected void saveAccessToken(PortletRequest req, PortletResponse res, AccessToken accessToken) {
    req.getPortletSession().setAttribute(ATTRIBUTE_ACCESS_TOKEN, accessToken);

    // Update existing access token in DB if it's different from the validated access token. It
    // could be the case with
    // Google when
    // token could be refreshed.
    AccessToken existingAccessToken = getOAuthProvider().loadAccessToken(req.getRemoteUser());
    if (accessToken != null && !accessToken.equals(existingAccessToken)) {
      getOAuthProvider().saveAccessToken(req.getRemoteUser(), accessToken);
    }
  }
  // Validate obtained access token with usage of concrete OAuthProvider and save it to session if
  // it's valid
  protected AccessToken validateAccessToken(
      PortletRequest request,
      PortletResponse response,
      OAuthProvider oauthProvider,
      AccessToken accessToken)
      throws PortletException, IOException {
    AccessToken previousAccessToken =
        (AccessToken) request.getPortletSession().getAttribute(ATTRIBUTE_ACCESS_TOKEN);

    if (isValidationNeeded(accessToken, previousAccessToken)) {
      // Validate accessToken
      try {
        accessToken = getOAuthProvider().validateTokenAndUpdateScopes(accessToken);
      } catch (OAuthApiException oe) {
        String jspPage;
        if (oe.getExceptionCode() == OAuthApiExceptionCode.ACCESS_TOKEN_ERROR) {
          request.setAttribute(
              ATTRIBUTE_ERROR_MESSAGE,
              oauthProvider.getFriendlyName() + " access token is invalid.");
          request.setAttribute(ATTRIBUTE_OAUTH_PROVIDER, oauthProvider);
          jspPage = "/jsp/error/token.jsp";
        } else if (oe.getExceptionCode() == OAuthApiExceptionCode.IO_ERROR) {
          oe.printStackTrace();
          request.setAttribute(
              ATTRIBUTE_ERROR_MESSAGE, "I/O error happened. See server.log for more details");
          jspPage = "/jsp/error/error.jsp";
        } else {
          // Some unexpected error
          throw new PortletException(oe);
        }

        PortletRequestDispatcher prd =
            filterConfig.getPortletContext().getRequestDispatcher(jspPage);
        prd.include(request, response);
        return null;
      }

      if (!accessToken.equals(previousAccessToken)) {
        saveAccessToken(request, response, accessToken);
      }
    }

    return accessToken;
  }