@Before
 public void open() {
   savedContext = OAuth2ClientContextHolder.getContext();
   resource = new BaseOAuth2ProtectedResourceDetails();
   // Facebook and older specs:
   resource.setTokenName("bearer_token");
   fac = new OAuth2ClientHttpRequestFactory(new SimpleClientHttpRequestFactory(), resource);
 }
  public OAuth2AccessToken obtainNewAccessToken(OAuth2ProtectedResourceDetails details)
      throws UserRedirectRequiredException, AccessDeniedException {

    AuthorizationCodeResourceDetails resource = (AuthorizationCodeResourceDetails) details;
    OAuth2ClientContext context = OAuth2ClientContextHolder.getContext();

    if (context != null && context.getErrorParameters() != null) {

      // there was an oauth error...
      throw getSerializationService().deserializeError(context.getErrorParameters());

    } else if (context == null || context.getAuthorizationCode() == null) {

      throw getRedirectForAuthorization(resource, context);

    } else {

      return retrieveToken(getParametersForTokenRequest(resource, context), resource);
    }
  }
 @After
 public void close() {
   OAuth2ClientContextHolder.setContext(savedContext);
 }