line OAuth parameters for access to resource OAuthParameters params = new OAuthParameters().signatureMethod("HMAC-SHA1"). consumerKey("key").setToken("accesskey"); // OAuth secrets to access resource OAuthSecrets secrets = new OAuthSecrets().consumerSecret("secret").setTokenSecret("accesssecret"); // if parameters and secrets remain static, filter can be added to each web resource OAuthClientFilter filter = new OAuthClientFilter(client.getProviders(), params, secrets); // OAuth test server WebResource resource = client.resource("http://term.ie/oauth/example/request_token.php"); resource.addFilter(filter); String response = resource.get(String.class);
Example 2 (handling authorization flow):
OAuthClientFilter filter = new OAuthClientFilter( client.getProviders(), new OAuthParameters().consumerKey("key"), new OAuthSecrets().consumerSecret("secret"), "http://request.token.uri", "http://access.token.uri", "http://authorization.uri", new OAuthClientFilter.AuthHandler() {
@Override public void authorized(String token, String tokenSecret) { // store the access token for future use storeAccessToken(token, tokenSecret); }
@Override public String authorize(URI authorizationUri) { try { // ask user to authorize the app and enter the verification code // generated by the server String verificationCode = askUserToGoToAuthUriAuthorizeAndEnterVerifier(authorizationUri); return verificationCode; } catch (IOException ex) { throw new RuntimeException(ex); } } } ); // add the filter to the client client.addFilter(filter); // make calls to the protected resources (authorization is handled // by the filter (and the passed AuthHandler) as needed, transparently WebResource resource = client.resource("http://my.service.uri/items"); String response = resource.get(String.class);
@author Paul C. Bryan
@author Martin Matula