OAuth 1 parameter transmission oauth_signature URL encoding
I am having an issue with the OAuth not URL encoding oauth_signature for transmission using request URI query with version 1.0.0.RC2. The OAuth specification says it should be encoded but that does not seem to be happening.
The problem occurs most commonly when the oauth_signature parameter in the URL contains a "+". This is converted to a space by the server and the signature, which then causes the signatue that is calculated by the server to differ, for example:
Code:
# Parameter in URL
&oauth_signature=Dtg4ar/FKoTGOVwKbam+gcLtRs4=
# Server Output
Signature value generated by server= "Dtg4ar/FKoTGOVwKbam+gcLtRs4="
Signature value supplied by client = "Dtg4ar/FKoTGOVwKbam gcLtRs4="
I have tried serveral things including using an Interceptor but the HttpRequest there does not contain the parameter information. I would really appreciate any suggestions?
Yours Bart.
The code I am using is:
Code:
private ProtectedResourceDetails buildProtectedResource() {
BaseProtectedResourceDetails protectedResource = new BaseProtectedResourceDetails();
Properties properties = Utility.getProperties();
String consumerKey = properties.getProperty("consumerKey");
String consumerSecret = properties.getProperty("consumerSecret");
SharedConsumerSecret sharedConsumerSecret = new SharedConsumerSecret(consumerSecret);
protectedResource.setConsumerKey(consumerKey);
protectedResource.setSharedSecret(sharedConsumerSecret);
protectedResource.setUse10a(true);
protectedResource.setId(resourceId);
protectedResource.setAcceptsAuthorizationHeader(false);
return protectedResource;
}
private OAuthConsumerSupport buildConsumerSupport(final ProtectedResourceDetails protectedResource) {
CoreOAuthConsumerSupport consumerSupport = new CoreOAuthConsumerSupport();
consumerSupport.setStreamHandlerFactory(new DefaultOAuthURLStreamHandlerFactory());
consumerSupport.setProtectedResourceDetailsService(new ProtectedResourceDetailsService() {
@Override
public ProtectedResourceDetails loadProtectedResourceDetailsById(String s) {
return protectedResource;
}
});
return consumerSupport;
}
private URL getNewUrl(String hostname, String controller, String action) {
String urlString = "https://" + hostname + controller + action;
try {
return new URL(urlString);
}
catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
public String seekQuery(String hostname, String controller, String action) {
final ProtectedResourceDetails protectedResource = buildProtectedResource();
OAuthConsumerSupport consumerSupport = buildConsumerSupport(protectedResource);
URL url = getNewUrl(hostname, controller, action);
log.info("url: " + url.toString());
Map<String, OAuthConsumerToken> tokens = new Hashtable<String, OAuthConsumerToken>();
OAuthConsumerToken token = new OAuthConsumerToken();
tokens.put(resourceId, token);
OAuthSecurityContextImpl securityContext = new OAuthSecurityContextImpl();
securityContext.setAccessTokens(tokens);
OAuthSecurityContextHolder.setContext(securityContext);
OAuthRestTemplate restTemplate = new OAuthRestTemplate(protectedResource);
restTemplate.setSupport(consumerSupport);
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
interceptors.add( new SeekRestInterceptor());
restTemplate.setInterceptors(interceptors);
String result = restTemplate.getForObject(url.toString(), String.class);
return result;
}