@GET @Produces("text/plain") public String handle( @QueryParam("file") String file, @QueryParam("size") String size, @Context HttpContext hc) { OAuthServerRequest osr = new OAuthServerRequest(hc.getRequest()); OAuthSecrets secrets = new OAuthSecrets().consumerSecret("kd94hf93k423kf44").tokenSecret("pfkkdhi9sl3r4s00"); OAuthParameters params = new OAuthParameters().readRequest(osr); // ensure query parameters are as expected assertEquals(file, "vacation.jpg"); assertEquals(size, "original"); // ensure query parameters correctly parsed into OAuth parameters object assertEquals(params.getConsumerKey(), "dpf43f3p2l4k3l03"); assertEquals(params.getToken(), "nnch734d00sl2jdk"); assertEquals(params.getSignatureMethod(), "HMAC-SHA1"); assertEquals(params.getTimestamp(), "1191242096"); assertEquals(params.getNonce(), "kllo9940pd9333jh"); assertEquals(params.getVersion(), "1.0"); try { // verify the HMAC-SHA1 signature assertTrue(OAuthSignature.verify(osr, params, secrets)); } catch (OAuthSignatureException ose) { fail(ose.getMessage()); } return "PHOTO"; }
// follow the detour private static void authorizeToken(String username, String password) { System.out.println("Authenticating to OAuth token service\n"); WebResource resource = client.resource(TOKEN_SERVICE + "NoBrowserAuthorization"); MultivaluedMapImpl query = new MultivaluedMapImpl(); query.add("username", username); query.add("password", password); query.add("request_token", params.getToken()); String s = resource.queryParams(query).get(String.class); System.out.println("Authenticated ok\n"); }