0

I am new to write a Java client code to call RestFul web services. These web services using OAuth2.0 security. I have client-id and secret keys with me, but unable to call through Java program. How should I get access token out of this and use this token further for Web service API calls. Here is how I tried:

private static void authorizationProcess() throws ClientProtocolException, IOException {

    DefaultHttpClient httpClient = new DefaultHttpClient();

    HttpContext context = new BasicHttpContext();
    HttpGet httpGet = new HttpGet("https://www.ustream.tv/oauth2/authorize?response_type=token&client_id=671c71f800c17f1d57f10eebeba2f42d230143cddji8&redirect_uri=www.ustream.tv");
    try {
        HttpResponse httpResponse = httpClient.execute(httpGet, context);
        System.out.println("Response status: " + httpResponse.getStatusLine());
        HttpRequest req = (HttpRequest) context.getAttribute(
                ExecutionContext.HTTP_REQUEST);
        System.out.println("Last request URI: " + req.getRequestLine());
        RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(
                DefaultRedirectStrategy.REDIRECT_LOCATIONS);
        if (redirectLocations != null) {
            System.out.println("All intermediate redirects: " + redirectLocations.getAll());
        }
        EntityUtils.consume(httpResponse.getEntity());
    } finally {
        httpGet.releaseConnection();
    }
}
}

Its giving Bad Request error message:

Response status: HTTP/1.1 400 Bad Request
Last request URI: GET /oauth2/authorize?response_type=token&client_id=671c71f800c17f1d57f10eebeba2f42d230143cddji8&    redirect_uri=www.ustream.tv HTTP/1.1
Sakuntala
  • 1
  • 2

2 Answers2

0

You're trying to execute an Authorization Code grant in Java code but that grant type was designed for real browser interaction. You should rather look in to a grant type such as Resource Owner Password Credentials or Client Credentials which both can be used with non-browser-based Clients.

Hans Z.
  • 50,496
  • 12
  • 102
  • 115
  • Yes you are correct. I tried grant type as Client Credentials and its working fine now from Standalone java application. – Sakuntala Sep 06 '16 at 12:24
0

I cannot comment to the Hans Z. answer, but I think it's actually an authorization request for Implicit grant (response_type=token is used, details here). It could be Android client for example. Some APIs requires http or https in the redirect_uri, which is missing in your example. You may give it a shot (keep in mind the Percent-encoding too)..

Community
  • 1
  • 1
Tomas Mano
  • 29
  • 5
  • Thanks for replying. Now I am able to write and execute the client code from a standalone java application. – Sakuntala Sep 06 '16 at 12:28