If you come across problems with using OAuth-Signpost and HTTP 411 errors, then you may want to check whether your service provider allows requests via POST. As I found out with writing my Hattrick application, the latest version of OAuth-Signpost is hardcoded to use POST with the DefaultOAuthProvider despite having the javadoc saying it defaults to GET.
As Hattrick only allows GET requests, I had to modify the DefaultOAuthProvider code to default to GET but I also added a method to change the request type:
private String requestMethod = "GET";
protected HttpRequest createRequest(String endpointUrl) throws MalformedURLException,
IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(endpointUrl).openConnection();
connection.setRequestMethod(requestMethod);
connection.setAllowUserInteraction(false);
connection.setRequestProperty("Content-Length", "0");
return new HttpURLConnectionRequestAdapter(connection);
}
public void setRequestMethod(String requestMethod) {
if (requestMethod != null && !"".equals(requestMethod)) {
this.requestMethod = requestMethod;
}
}
This solved my 411 errors and haven’t looked back since.
Advertisement
Despite what the documentation says, could I ask you why you are not using the CommonsHTTPOAuthProvider? I’m writing an Hattrick application too. I manage the OAuth protocol with signpost and using CommonsHTTPOAuthProvider without problem.
Are you using signpost 1.2.1 from their git repo? The problem is in the Signpost code. A change in 1.2 -> 1.2.1 changed the http call method to post which is rejected by hattrick, so if you’re using 1.2 then there is no problem.
Hi, I’m writing an hattrick application for android using signpost library but I don’t know how get the request token. Can you post a code in Java to make this? Thank you.
Unfortunately, I don’t have time to sit down and write a class for you. However, what I did was mainly use the examples that are present with the source code. I based my own OAuth manager on the example – you should be able to do the same.
Ok. But I want the example how i can write the request for the request token not the entire class.
With your solution I don’t understand how I can apply.
To get the request token:
try {
OAuthConsumer consumer = new DefaultOAuthConsumer(CHPP_CONSUMER_KEY, CHPP_CONSUMER_SECRET);
OAuthProvider provider = new DefaultOAuthProvider("http://chpp.hattrick.org/oauth/request_token.ashx",
"http://chpp.hattrick.org/oauth/access_token.ashx",
"http://chpp.hattrick.org/oauth/authorize.aspx");
String url = provider.retrieveRequestToken(consumer, "oob");
} catch (OAuthMessageSignerException e) {
System.err.println("Error: " + e.getMessage());
} catch (OAuthNotAuthorizedException e) {
System.err.println("Error: " + e.getMessage());
} catch (OAuthExpectationFailedException e) {
System.err.println("Error: " + e.getMessage());
} catch (OAuthCommunicationException e) {
System.err.println("Error: " + e.getMessage());
}
This returns a URL in Hattrick you need to visit to get the authorisation code.
And then to authorise it, you do this:
try {if (verificationCode != null && !"".equals(verificationCode)) {
provider.retrieveAccessToken(consumer, verificationCode);
}
} catch (OAuthMessageSignerException e) {
System.err.println("Error: " + e.getMessage());
} catch (OAuthNotAuthorizedException e) {
System.err.println("Error: " + e.getMessage());
} catch (OAuthExpectationFailedException e) {
System.err.println("Error: " + e.getMessage());
} catch (OAuthCommunicationException e) {
System.err.println("Error: " + e.getMessage());
}
String token = consumer.getToken();
String tokenSecret = consumer.getTokenSecret();
The tokens at the end are what you use to sign the requests to Hattrick for data. If you don’t understand any of this, you need to go read the documentation for Signpost as that’s where I got it from.
I don’t understand the meaning of “String endpointUrl”. (i’m writing app for hattrick so what it is the endpintUrl?)
The endpointUrl is the URL which you’re contacting, so for Hattrick, it’s the http://chpp.hattrick.org one.