Last active
December 20, 2015 12:29
-
-
Save PeterHancock/6131763 to your computer and use it in GitHub Desktop.
OAUTH client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@GrabConfig(systemClassLoader=true) | |
@GrabResolver(name='org.climatic', root='http://dl.bintray.com/content/climatic/climatic') | |
@Grab('com.sun.jersey:jersey-core:1.10') | |
@Grab('com.sun.jersey:jersey-client:1.10') | |
@Grab('com.sun.jersey.contribs.jersey-oauth:oauth-signature:1.10') | |
@Grab('com.sun.jersey.contribs.jersey-oauth:oauth-client:1.10') | |
@Grab('org.climatic:climatic-core:0.1.2') | |
import com.sun.jersey.api.client.Client | |
import com.sun.jersey.api.client.ClientResponse | |
import com.sun.jersey.oauth.signature.OAuthSecrets | |
import com.sun.jersey.oauth.signature.OAuthParameters | |
import com.sun.jersey.oauth.client.OAuthClientFilter | |
import com.sun.jersey.api.client.filter.LoggingFilter | |
import javax.ws.rs.core.HttpHeaders | |
import javax.ws.rs.core.MediaType | |
import com.sun.jersey.api.representation.Form | |
import org.climatic.TaskApp | |
def createResource(config) { | |
def client = Client.create() | |
def params = configureParams(config) | |
def secrets = new OAuthSecrets().consumerSecret(config.secret) | |
def resource = client.resource(config.url) | |
resource.addFilter(new LoggingFilter(System.out)) | |
resource.addFilter(new OAuthClientFilter(client.getProviders(), params, secrets)) | |
resource.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON) | |
} | |
def configureParams(config) { | |
def params = new OAuthParameters().consumerKey(config.key). | |
signatureMethod("HMAC-SHA1").version("1.0") | |
config.nonce.with { nonce -> | |
if (nonce) { | |
params.nonce nonce | |
} | |
} | |
config.timestamp.with { timestamp -> | |
if (timestamp) { | |
params.timestamp timestamp | |
} | |
} | |
params | |
} | |
def deserializeParams(paramsString) { | |
paramsString.split(';').findAll { it } | |
.collect{ it.split('=') } | |
.inject([:]) { params, param -> | |
def (name, value) = param | |
params[name.trim()] = value.trim() | |
params | |
} | |
} | |
// Script | |
new TaskApp('oauth-test').configure { | |
description 'poll request - first arg is the required gid' | |
configureCliBuilder { cliBuilder -> | |
cliBuilder.with { | |
u longOpt: 'url', args:1, argName:'url', 'e.g http:localhost:8080' | |
k longOpt: 'key', args:1, 'oauth consumer key' | |
s longOpt: 'secret', args:1, 'oauth consumer secret' | |
n longOpt: 'nonce', args:1, 'Optional oauth nonce' | |
t longOpt: 'timestamp', args:1, 'Optional oauth timestamp' | |
} | |
} | |
handleOptions { options, config -> | |
if (!options.u) { | |
help 'url is required' | |
} | |
if (!options.k) { | |
help 'no key' | |
} | |
if (!options.s) { | |
help 'no secret' | |
} | |
config.url = options.u | |
config.key = options.k | |
config.secret = options.s | |
if (options.n) { | |
config.nonce = options.n | |
} | |
if (options.t) { | |
config.timestamp = options.t | |
} | |
} | |
task('get') { | |
description 'GET request.' | |
onExecute { task, config, args -> | |
createResource(config).get(String.class) | |
} | |
} | |
task('post') { | |
description 'POST request. Required arg of the form "name1=val1;name2=val2"' | |
onExecute { task, config, args -> | |
def form = new Form() | |
def postParams = deserializeParams(args[0]) | |
postParams.each { name, value -> form.add name, value } | |
createResource(config).type(MediaType.APPLICATION_FORM_URLENCODED) | |
.post(ClientResponse.class, form) | |
} | |
} | |
}.run(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment