@Override
 protected void storeToken(String tokenValue, OAuthProviderTokenImpl token) {
   String userName = token.getUserAuthentication().getName();
   OAuthProviderTokenImplWithService token2 = (OAuthProviderTokenImplWithService) token;
   com.ccc.oauth.apimanagement.model.Service service = token2.getService();
   if (token.isAccessToken()) {
     AccessKeyForUser access = new AccessKeyForUser();
     access.setService(service);
     access.setUserName(userName);
     access.setObtained(new Timestamp(token2.getTimestamp()));
     access.setAccessCode(token.getValue());
     addKeyForUser(access);
   } else {
     OAuth2KeyUser key = new OAuth2KeyUser();
     key.setCode(token2.getValue());
     key.setUserName(userName);
     key.setTimeMade(new Timestamp(token.getTimestamp()));
     key.setService(service);
     addKeyForUser(key);
   }
 }
  public boolean processRedirect(
      HttpServletRequest request,
      HttpServletResponse response,
      String token,
      String error,
      String errorMessage,
      String oauth1Token,
      String oauthVerifier,
      com.ccc.oauth.apimanagement.model.Service s,
      String userName) {
    if (log.isDebugEnabled()) {
      log.debug(
          "Code {} token {} error {} error descriptor {}  error message {} oauthtoken {} oauthverifer {} ",
          new Object[] {token, token, error, errorMessage, oauth1Token, oauthVerifier});
    }

    if (error != null && errorMessage != null) {
      log.warn(
          "Error found when retrieving code: "
              + " Error type: "
              + error
              + " and message: "
              + errorMessage);
    }
    OAuth2KeyUser key = null;

    List<AuthSupports> supported = supportedAuthForService(s);
    for (AuthSupports supports : supported) {
      if (supports.getAuthType().getName().equals(HttpAuthUtils.OAUTH2)) {

        key = new OAuth2KeyUser();
        key.setUserName(userName);
        key.setService(s);
        key.setTimeMade(new Timestamp(System.currentTimeMillis()));
        key.setCode(token);
        if (log.isDebugEnabled()) {
          log.debug("Added new key for: " + userName + " and service: " + s.getName());
        }

        Assert.isTrue(addKeyForUser(key));
        AccessKeyForUser accessKey =
            HttpAuthUtils.requestAccessWithLookup(this, userName, s.getId());
        if (log.isDebugEnabled()) {
          log.debug(
              "Requested to find access key: " + accessKey == null
                  ? " not found: "
                  : accessKey.getAccessCode());
        }

        if (accessKey == null) {
          log.warn("Failed to retrieve access key");
          return false;
        }

        Assert.isTrue(addKeyForUser(accessKey), "Couldn't add key for user: "******"Attempting oauth authorization");
          }

          String toSave = null;
          // 1.0a compliance
          if (oauthVerifier != null && oauth1Token != null) toSave = oauthVerifier;
          else if (oauthVerifier != null) toSave = oauthVerifier;
          else if (oauth1Token != null) toSave = oauth1Token;

          if (toSave != null) {

            requestToken.setRequestToken(toSave);
          } else {
            if (log.isDebugEnabled()) {
              log.debug(
                  "Attempted to save oauth 1 token for service: {} but couldn't find token passed in  for user {}",
                  s.getName(),
                  userName);
            }
          }
          if (log.isDebugEnabled()) {
            log.debug("Found request token: " + toSave);
          }
          updateRequestToken(requestToken);
        }

        OAuth1Info oauth1Info = oauthInfoForService(s);

        try {
          String accessToken = HttpAuthUtils.getAccessTokenFromPin(oauth1Info, this, requestToken);
          if (accessToken == null) log.warn("Error retrieving access token for {}", s.getName());

          log.info("Access token: " + accessToken);
        } catch (OAuthMessageSignerException e) {
          log.error("Error oauth1 token: ", e);
          return false;
        } catch (OAuthNotAuthorizedException e) {
          log.error("Error oauth1 token: ", e);
          return false;
        } catch (OAuthExpectationFailedException e) {
          log.error("Error oauth1 token: ", e);
          return false;
        } catch (OAuthCommunicationException e) {
          log.error("Error oauth1 token: ", e);
          return false;
        }
      }
    }
    return true;
  }