Signpost with Friendfeed

58 views
Skip to first unread message

monkeyvu

unread,
Aug 2, 2009, 8:12:11 PM8/2/09
to Signpost users
Hi all,

I'm using Signpost for Friendfeed Oauth. It looks like they dont
support the PIN code so I just ignore it. Then I tried to send a
request to http://friendfeed-api.com/v2/feed/vupham and all are good
as below:

Fetching request token from Friendfeed...
Request token: 700274c45fd644e787c1495c12376c40
Token secret: 8d7ae8ed510b48d88eed14879e3526bc
Now visit:
https://friendfeed.com/account/oauth/authorize?oauth_token=700274c45fd644e787c1495c12376c40&oauth_callback=oob
... and grant this app authorization
Enter the PIN code and hit ENTER when you're done:

Fetching access token from Friendfeed...
Access token: d747c3df9c87419c9845f1cdbd3a0893
Token secret: 277ea532336741f2a98dc7af770af06e
Sending request to Friendfeed...
Response: 200 OK


After that I tried to post a message as below:

String urlParameters = "body=Hello man";
URL url = new URL("http://friendfeed-api.com/v2/entry");
HttpURLConnection request = (HttpURLConnection) url
.openConnection();



request.setRequestMethod("POST");

request.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
request.setRequestProperty("Content-Length", ""
+ Integer.toString(urlParameters.getBytes
().length));

request.setDoInput(true);
request.setDoOutput(true);

consumer.sign(request);

DataOutputStream wr = new DataOutputStream(request
.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

InputStream is = request.getInputStream();
BufferedReader rd = new BufferedReader(new
InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
System.out.println(response.toString());


And what I got is an exception:

java.io.IOException: Server returned HTTP response code: 401 for URL:
http://friendfeed-api.com/v2/entry
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown
Source)
at au.kdts.test.TestFriendfeed.<init>(TestFriendfeed.java:55)
at au.kdts.test.TestFriendfeed.main(TestFriendfeed.java:78)

Do you know what the issue is? Please help me, thanks very much.

matthias

unread,
Aug 3, 2009, 12:31:01 PM8/3/09
to Signpost users
Hi,

yes, this is in fact a problem with message signing and the way Java
URLConnection works. I didn't yet find a decent solution to this, and
I should have probably mentioned in the wiki that using URLConnection
for anything other than GET does not work with Signpost.

Here's why:
When sending data using HttpURLConnection, the payload never becomes
part of the request until you actually send it over the wire using the
writer. That's because URLConnection is entirely modeled around data
streams. This is a problem, because for Signpost, there is no way to
actually look at the message payload and consider potential parameters
in there for inclusion into the signature, for the simple reason that
when you sign the message, there simply isn't yet a payload to
inspect.

I'm not sure whether there is a good solution for this, I couldn't yet
think of any so far. However, when doing advanced things like POSTing
data to a web service using signed requests, you really, REALLY want
to consider using a more elaborate HTTP messaging library anyway (I
would recommend using Apache HttpComponents). Signpost supports Apache
HttpComponents and Jetty HTTP for these occasions.

I mean, you can see how messy the code for sending a POST or PUT using
HttpURLConnection is, so you're better off using something decent
instead anyway.

But, I admit that this is very confusing (especially since it simply
yields a 401), so maybe I'll open a ticket for this, to either find a
good solution/workaround or simply update the docs that signing
URLConnection type requests carrying payload is simply not supported.

On Aug 3, 2:12 am, monkeyvu <phamanhvu101...@gmail.com> wrote:
> Hi all,
>
> I'm using Signpost for Friendfeed Oauth. It looks like they dont
> support the PIN code so I just ignore it. Then I tried to send a
> request tohttp://friendfeed-api.com/v2/feed/vuphamand all are good
> as below:
>
> Fetching request token from Friendfeed...
> Request token: 700274c45fd644e787c1495c12376c40
> Token secret: 8d7ae8ed510b48d88eed14879e3526bc
> Now visit:https://friendfeed.com/account/oauth/authorize?oauth_token=700274c45f...
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
0 new messages