Deemoore / oauth-signpost

Automatically exported from code.google.com/p/oauth-signpost
0 stars 0 forks source link

didn't support HTTP GET method in this framework #65

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
The DefaultOAuthProvider and the DefaultOAuthConsumer only support HTTP POST 
method. But in some situation we need HTTP GET method in the oauth process. So 
we have to extends the AbstractOAuthProvider class and the 
AbstractOAuthConsumer class by ourself. I think it is a necessary to extends 
the two class by the signpost framework so we can just use it by defalut. What 
do you think? Thank you.
The code below maybe can help your gays.
-----------------------------------------------------------------------
import java.net.HttpURLConnection;
import oauth.signpost.AbstractOAuthConsumer;
import oauth.signpost.basic.UrlStringRequestAdapter;
import oauth.signpost.http.HttpRequest;

public class UrlOAuthConsumer extends AbstractOAuthConsumer {
    private static final long serialVersionUID = 123L;

    public UrlOAuthConsumer(String consumerKey, String consumerSecret) {
        super(consumerKey, consumerSecret);
    }

    @Override
    protected HttpRequest wrap(Object request) {

        if (!(request instanceof HttpURLConnection)) {
            throw new IllegalArgumentException(
                    "The default consumer expects requests of type java.net.HttpURLConnection");
        }
        return new UrlStringRequestAdapter(((HttpURLConnection) request)
                .getURL().toString());

    }
}
-----------------------------------------------------------------------
import java.net.HttpURLConnection;
import java.net.URL;
import oauth.signpost.AbstractOAuthProvider;
import oauth.signpost.basic.HttpURLConnectionResponseAdapter;
import oauth.signpost.basic.UrlStringRequestAdapter;
import oauth.signpost.http.HttpRequest;
import oauth.signpost.http.HttpResponse;

public class UrlOAuthProvider extends AbstractOAuthProvider {
    private static final long serialVersionUID = 1323232L;

    public UrlOAuthProvider(String requestTokenEndpointUrl,
            String accessTokenEndpointUrl, String authorizationWebsiteUrl) {
        super(requestTokenEndpointUrl, accessTokenEndpointUrl,
                authorizationWebsiteUrl);
    }

    @Override
    protected HttpRequest createRequest(String url) throws Exception {
        return new UrlStringRequestAdapter(url);
    }

    @Override
    protected HttpResponse sendRequest(HttpRequest request) throws Exception {
        HttpURLConnection connection = (HttpURLConnection) new URL(
                (String) request.unwrap()).openConnection();
        connection.setRequestMethod(request.getMethod());
        connection.connect();
        return new HttpURLConnectionResponseAdapter(connection);
    }

}

Original issue reported on code.google.com by cndoublehero@gmail.com on 16 Jan 2011 at 7:50