Problem with Post calls

31 views
Skip to first unread message

Codomaniac

unread,
Oct 12, 2010, 2:27:39 AM10/12/10
to Brightkite API
Hi.
I am trying to post comment on an Object (that is, a post, checkin,
photopost etc)
The problem is that a Post call with out a parameter works (this too
faces trouble at times), but the calls with Parameters don't work and
i get 417 Http Status or at times 500.

for example, here is what i am doing for testing purposes


HttpPost request=new HttpPost("http://brightkite.com/places/
4d65bf3e648846b767bc58e6fc5cee12/checkins.xml");
consumer.sign(request);

List <NameValuePair> parameters = new
ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("comment[comment]", "I'm
hopeful"));

UrlEncodedFormEntity sendentity = new
UrlEncodedFormEntity(parameters, HTTP.UTF_8);

request.setHeader("Content-Type","application/x-www-form-
urlencoded");
request.setEntity(sendentity);

HttpResponse response=httpClient.execute(request);

int status=response.getStatusLine().getStatusCode();

System.out.println("Returned status -> "+status);

InputStream is2= response.getEntity().getContent();
for (int i = is2.read(); i > -1; i = is2.read()) {
System.out.write(i);
}
------------------------------------------------------------------------------------------------------------------------------------------------

What am i doing wrong? I have tried many things but the most
appropriate to me is what i have shared here. Any help would be great.
I have developed a Java wrapper for Brightkite Api, however, this is
the only thing missing (i.e posting comments and creating objects). By
the way Likes call doesn't work too, I get a success response but
Likes don't appear !

LJHarb

unread,
Oct 12, 2010, 2:31:54 AM10/12/10
to Brightkite API
Hi -

Signing the OAuth call means you're ensuring that after it's been
signed, the call does not change (that's the security OAuth provides -
preventing tampering with authenticated calls). Thus, you have to add
your parameters BEFORE you sign the OAuth call - adding a parameter
afterwards is tantamount to tampering with the call.

Let me know if this helps.

- Jordan

Codomaniac

unread,
Oct 12, 2010, 2:33:02 AM10/12/10
to Brightkite API
and By the way "consumer" is

OAuthConsumer consumer=new
CommonsHttpOAuthConsumer(ConsumerKey,ConsumerSecret);

Codomaniac

unread,
Oct 12, 2010, 8:31:36 AM10/12/10
to Brightkite API
Indeed you are right, i must have forgotten to post the correct code.
However, doing what you told me to , I get following response ...

Returned status -> 417
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://
www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html;
charset=iso-8859-1">
<TITLE>ERROR: The requested URL could not be retrieved</TITLE>
<STYLE type="text/css"><!--BODY{background-color:#ffffff;font-
family:verdana,sans-serif}PRE{font-family:sans-serif}--></STYLE>
</HEAD><BODY>
<H1>ERROR</H1>
<H2>The requested URL could not be retrieved</H2>
<HR noshade size="1px">
<P>
While trying to process the request:
<PRE>
POST /places/4d65bf3e648846b767bc58e6fc5cee12/checkins.xml HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Authorization: OAuth
oauth_token=&quot;NTQ5ZWEzYWQ1NmVmNDM5ZjczNzJkMGEx&quot;,
oauth_consumer_key=&quot;M2Y1ODYxYjI1ZDkzMmY0OGI0OGY0ZTU4&quot;,
oauth_version=&quot;1.0&quot;, oauth_signature_method=&quot;HMAC-
SHA1&quot;, oauth_timestamp=&quot;1286886342&quot;,
oauth_nonce=&quot;-1429113925153201057&quot;,
oauth_signature=&quot;MjotZ5KUecMoiQrP5cq3uRwQ8Kk%3D&quot;
Content-Length: 34
Host: brightkite.com
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.0-beta1 (java 1.4)
Expect: 100-Continue

</PRE>
<P>
The following error was encountered:
<UL>
<LI>
<STRONG>
Invalid Request
</STRONG>
</UL>

<P>
Some aspect of the HTTP Request is invalid. Possible problems:
<UL>
<LI>Missing or unknown request method
<LI>Missing URL
<LI>Missing HTTP Identifier (HTTP/1.0)
<LI>Request is too large
<LI>Content-Length missing for POST or PUT requests
<LI>Illegal character in hostname; underscores are not allowed
</UL>
<P>Your cache administrator is <A HREF="mailto:root">root</A>.

<BR clear="all">
<HR noshade size="1px">
<ADDRESS>
Generated Tue, 12 Oct 2010 12:24:59 GMT by gateway.csquareonline.local
(squid/2.7.STABLE6)
</ADDRESS>
</BODY></HTML>

......... Corrected code..........

HttpPost request=new HttpPost("http://brightkite.com/places/
4d65bf3e648846b767bc58e6fc5cee12/checkins.xml");

List <NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("comment[comment]", "xyz
comment"));

UrlEncodedFormEntity sendentity = new UrlEncodedFormEntity(parameters,
HTTP.UTF_8);
request.setHeader("Content-Type","application/x-www-form-urlencoded");
request.setEntity(sendentity);

consumer.sign(request);

LJHarb

unread,
Oct 12, 2010, 2:44:59 PM10/12/10
to Brightkite API
It looks like you're trying to post a comment to a place - comments
can only be posted on objects. Objects are checkins, posts, tips, etc.

Check out http://api.brightkite.com/objects.html for the parameters
you need to do a correct checkin.

- Jordan

On Oct 12, 5:31 am, Codomaniac <mauze_muaw...@hotmail.com> wrote:
> Indeed you are right, i must have forgotten to post the correct code.
> However, doing what you told me to , I get following response ...
>
> Returned status -> 417
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
> <HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html;
> charset=iso-8859-1">
> <TITLE>ERROR: The requested URL could not be retrieved</TITLE>
> <STYLE type="text/css"><!--BODY{background-color:#ffffff;font-
> family:verdana,sans-serif}PRE{font-family:sans-serif}--></STYLE>
> </HEAD><BODY>
> <H1>ERROR</H1>
> <H2>The requested URL could not be retrieved</H2>
> <HR noshade size="1px">
> <P>
> While trying to process the request:
> <PRE>
> POST /places/4d65bf3e648846b767bc58e6fc5cee12/checkins.xml HTTP/1.1
> Content-Type: application/x-www-form-urlencoded
> Authorization: OAuth
> oauth_token="NTQ5ZWEzYWQ1NmVmNDM5ZjczNzJkMGEx",
> oauth_consumer_key="M2Y1ODYxYjI1ZDkzMmY0OGI0OGY0ZTU4",
> oauth_version="1.0", oauth_signature_method="HMAC-
> SHA1", oauth_timestamp="1286886342",
> oauth_nonce="-1429113925153201057",
> oauth_signature="MjotZ5KUecMoiQrP5cq3uRwQ8Kk%3D"

Codomaniac

unread,
Oct 13, 2010, 12:50:33 AM10/13/10
to Brightkite API
That wasn't working, i had checked it too(as i said, i have tried many
things). In the following piece of code i am trying to comment on a
checkin

<checkin>
<watched type="boolean">false</watched>
<object_type>checkin</object_type>
<share_with>only me</share_with>
<tip type="boolean">false</tip>
<ratings_count type="integer">0</ratings_count>
<via>web</via>
<public type="boolean">false</public>
<creator>
<fullname>T-Rex</fullname>
<blocking_me type="boolean">false</blocking_me>
<avatar_url>http://brightkite.com/images/
default_user_avatar.png</avatar_url>
<login>khaptman</login>
<small_avatar_url>http://brightkite.com/images/
default_user_avatar-small.png</small_avatar_url>
<smaller_avatar_url>http://brightkite.com/images/
default_user_avatar-smaller.png</smaller_avatar_url>
<tiny_avatar_url>http://brightkite.com/images/
default_user_avatar-tiny.png</tiny_avatar_url>
<id>0f017c81665fba5729d2789c3bf7f58e</id>
</creator>
<is_public type="boolean">false</is_public>
<rating type="integer">0</rating>
<comments_count type="integer">0</comments_count>
<created_at_with_tz>Thursday, September 23, 2010 05:04:23Z</
created_at_with_tz>
<place>
<scope>address</scope>
<latitude type="float">24.906667</latitude>
<display_location>Karachi, Pakistan</display_location>
<longitude type="float">67.160833</longitude>
<name>Quaid-e-azam Intl</name>
<id>416ba9feae62798858c66b9cc55b5fa8</id>
</place>
<view_count type="integer">61</view_count>
<created_at_ts>1285218263.327000</created_at_ts>
<id>aa6f52d903ec8407417238a609314a2b</id>
<created_at_as_words>19 days</created_at_as_words>
<created_at type="datetime">2010-09-23T05:04:23Z</created_at>
<about type="boolean">false</about>
</checkin>


Id of this checkin is "aa6f52d903ec8407417238a609314a2b" .... when I
try to comment on it (with following code)



HttpPost request=new HttpPost("http://brightkite.com/objects/
aa6f52d903ec8407417238a609314a2b/comments.xml");

List <NameValuePair> parameters = new
ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("comment[comment]", "xyz
comment"));

UrlEncodedFormEntity sendentity = new
UrlEncodedFormEntity(parameters, HTTP.UTF_8);
request.setEntity(sendentity);

request.setHeader("Content-Type","application/x-www-form-
urlencoded");

consumer.sign(request);

HttpResponse response=httpClient.execute(request);

int status=response.getStatusLine().getStatusCode();

System.out.println("Returned status -> "+status);

InputStream is2= response.getEntity().getContent();
for (int i = is2.read(); i > -1; i = is2.read()) {
System.out.write(i);
}


-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
I get the same error....

Returned status -> 417
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://
www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html;
charset=iso-8859-1">
<TITLE>ERROR: The requested URL could not be retrieved</TITLE>
<STYLE type="text/css"><!--BODY{background-color:#ffffff;font-
family:verdana,sans-serif}PRE{font-family:sans-serif}--></STYLE>
</HEAD><BODY>
<H1>ERROR</H1>
<H2>The requested URL could not be retrieved</H2>
<HR noshade size="1px">
<P>
While trying to process the request:
<PRE>
POST /objects/aa6f52d903ec8407417238a609314a2b/comments.xml HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Authorization: OAuth
oauth_token=&quot;MGNjMTFhMzFiMTExY2JjNGMzM2VmZDVh&quot;,
oauth_consumer_key=&quot;M2Y1ODYxYjI1ZDkzMmY0OGI0OGY0ZTU4&quot;,
oauth_version=&quot;1.0&quot;, oauth_signature_method=&quot;HMAC-
SHA1&quot;, oauth_timestamp=&quot;1286945372&quot;, oauth_nonce=&quot;
7549350979876047373&quot;,
oauth_signature=&quot;egZq3RGEYiCpU3ZqSBgeaGRzX%2F4%3D&quot;
Generated Wed, 13 Oct 2010 04:48:45 GMT by gateway.csquareonline.local
(squid/2.7.STABLE6)
</ADDRESS>
</BODY></HTML>



-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

LJHarb

unread,
Oct 15, 2010, 1:26:49 PM10/15/10
to Brightkite API
I can attest that the OAuth processes are working, and that the API is
working - if you wish to verify for testing purposes, try just
explicitly authenticating (meaning, don't use OAuth, just include
`username` and `password` parameters.

If that call works, then the problem is in your OAuth library and
implementation. It looks like you're using http://code.google.com/p/oauth-signpost/
- I notice the example code differs from your example:

// create an HTTP request to a protected resource
URL url = new URL("http://api.example.com/protected")
HttpURLConnection request = (HttpURLConnection)
url.openConnection();

// sign the request (consumer is a Signpost
DefaultOAuthConsumer)
consumer.sign(request);

// send the request
request.connect();

Specifically, the example says to use request.connect, while your code
uses httpClient.execute(request). Perhaps this is the issue?

- Jordan
> oauth_token="MGNjMTFhMzFiMTExY2JjNGMzM2VmZDVh",
> oauth_consumer_key="M2Y1ODYxYjI1ZDkzMmY0OGI0OGY0ZTU4",
> oauth_version="1.0", oauth_signature_method="HMAC-
> SHA1", oauth_timestamp="1286945372", oauth_nonce="
> 7549350979876047373",
> oauth_signature="egZq3RGEYiCpU3ZqSBgeaGRzX%2F4%3D"

Codomaniac

unread,
Oct 18, 2010, 12:54:33 AM10/18/10
to Brightkite API
Now, as I did what u told me (following is the code for it)

URL url=new URL("http://brightkite.com/objects/
aa6f52d903ec8407417238a609314a2b/comments.xml");
HttpURLConnection conn = (HttpURLConnection)
url.openConnection();

conn.setRequestProperty("comment[comment]", "xyz comment");

conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Length",
String,valueOf(conn.getContentLength()));


//Brighkiteproperties is my custom bean and it has the tokens etc
DefaultOAuthConsumer consumer=new
DefaultOAuthConsumer(brightKiteProperties.getConsumerKey(),brightKiteProperties.getConsumerSecret());

consumer.setTokenWithSecret(brightKiteProperties.getAccessToken(),
brightKiteProperties.getToken_secret());
consumer.sign(conn);
InputStream is= conn.getInputStream();

for (int i = is.read(); i > -1; i = is.read()) {
System.out.write(i);
}


I have received in response !

java.lang.IllegalStateException: Already connected
at
sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:
2235)
at test.classes.testClass.URLStyle(testClass.java:300)
at test.classes.testClass.main(testClass.java:249)

Codomaniac

unread,
Oct 19, 2010, 7:43:13 AM10/19/10
to Brightkite API
I missed one line

//placed after signing
conn.connect();

and the error is the same !

LJHarb

unread,
Oct 19, 2010, 1:36:07 PM10/19/10
to Brightkite API
At this point, I think the issues are in your code/library and not
with our API, so this group is no longer the best place to discuss it.
However, I'm still more than welcome to help you work through it -
please email me directly at < jor...@brightkite.com >.

If it turns out there's any issue with the API, or if we come up with
useful information for other API developers, I'll post it here so it
can help others.

- Jordan

Codomaniac

unread,
Oct 20, 2010, 7:00:00 AM10/20/10
to Brightkite API
It works now, but sadly i used (unwillingly :D) my Username and
password. Now I am trying to do it with OAuth, in the mean time any
help would be nice !


import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

HttpClient client = new HttpClient();
BufferedReader br = null;

PostMethod method = new PostMethod("http://brightkite.com/objects/
aa6f52d903ec8407417238a609314a2b/comments.xml");

method.setParameter("comment[comment]","This works!");

final String auth ="username:password";
method.setRequestHeader("Authorization","Basic "+
Base64.encodeBytes(auth.getBytes()).trim());

try{

int returnCode = client.executeMethod(method);

if(returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
System.err.println("The Post method is not implemented by
this URI");
// still consume the response body
method.getResponseBodyAsString();
} else {
br = new BufferedReader(new
InputStreamReader(method.getResponseBodyAsStream()));
String readLine;
while(((readLine = br.readLine()) != null)) {
System.err.println(readLine);
}
}
} catch (Exception e) {
System.err.println(e);
} finally {
method.releaseConnection();
if(br != null) try { br.close(); } catch (Exception fe) {}
}

Codomaniac

unread,
Oct 22, 2010, 2:00:02 AM10/22/10
to Brightkite API

which OAuth do i use for this ? I know two implementations,
CommonsHttpOAuthConsumer and DefaultOAuthConsumer. However, I can't
seem to find any class that signs a POSTMETHOD object ?

Codomaniac

unread,
Oct 25, 2010, 9:16:13 AM10/25/10
to Brightkite API
http://support.brightkite.com/discussions/problems/88-oauth-doesnt-work


here is something similar to what I am asking !!!

LJHarb

unread,
Oct 25, 2010, 6:52:26 PM10/25/10
to Brightkite API
Unfortunately I'm not a Java developer, so all I can do is Google to
see if I can help.

http://developer.linkedin.com/thread/1442 seems similar to your issue.

Please email me directly at jor...@brightkite.com for further
discussion on this topic. If the API functions, and the problem is in
your implementation - although I will be happy to help you via email -
the Google Group is not an appropriate place for figuring out client-
side implementation issues.

Thanks!

- Jordan
Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages