1

I am trying integrate my current project with a external authentication API, and right now my goal it is redirect to a external url:

https://auth.mercadolivre.com.br/authorization?response_type=code&client_id=$APP_ID

where the autorization process takes place, after that it's redirect back to my application, with an url like that:

http://YOUR_REDIRECT_URI?code=SERVER_GENERATED_AUTHORIZATION_CODE

where I need store this code variable internally.

I got this code so far, based on the examples available here and here:

public String getCode() throws ApiException, URISyntaxException {
  Client client = ClientBuilder.newClient();
  WebTarget resourceTarget = client.target(getApi().getLocation());

  // Build a HTTP GET request that accepts "text/plain" response type
  // and contains a custom HTTP header entry "Foo: bar".
  Invocation invocation = resourceTarget.request("text/plain").buildGet();

  // Invoke the request using generic interface
  String response = invocation.invoke(String.class);
  return response;
}

@POST
public Response getApi() throws ApiException, URISyntaxException {
  getAuthUrl();
  URI targetURIForRedirection = new URI(auth_url);
  return Response.temporaryRedirect(targetURIForRedirection).build();
}

But, despite the application reaching the destination, instead of being open in the browser, the html is dumped on the console and an error is issued (something like an invalid character on the code dumped on the console).

I just wan, from the methods above, redirect the user to the authorization page (first link), and when the process ends, execute the rest of the code, storing the value returned for future uses.

For reference, this code it is called from the AuthenticationManager in my spring-security layer. The implementation I got so far:

@Configuration
@EnableWebSecurity
public class Security extends WebSecurityConfigurerAdapter {
  @Bean
  @Override
  public AuthenticationManager authenticationManagerBean() throws Exception {
    return new AuthManager();
  }

  @Override
  public void configure(HttpSecurity http) throws Exception {
...
  }

  @Override
  public void configure(WebSecurity web) throws Exception {
...
  }

  public class AuthManager implements AuthenticationManager {
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
      MercadoLivre mercadoLivre = new MercadoLivre();
      try {
        mercadoLivre.getAccessToken();
        UserResponse data = (UserResponse) mercadoLivre.GET("/users/"+mercadoLivre.getUserId().toString());
        return new AuthResponse(data);
      } catch (Exception e) {
        e.printStackTrace();
        return null;
      }
    }
  }

  public class AuthResponse implements Authentication {
...
  }
}

the method is called from inside getAccessToken().

Kleber Mota
  • 8,521
  • 31
  • 94
  • 188

0 Answers0