String postBody="status="+urlEncode("Test message");
here is the base string i am using
String baseString = "POST&"
+ URLUTF8Encoder
.encode("http://api.twitter.com/1/statuses/update.xml")
+ "&oauth_consumer_key%3D" + ""My Consumer key""
+ "%26oauth_nonce%3D"+oauth_nonce
+
"%26oauth_signature_method%3D"+oauth_signature_method
+ "%26oauth_token%3D"+auth_token
+ "%26oauth_timestamp%3D"+oauth_timestamp
+ "%26oauth_version%3D"+oauth_version + "%26"
+ URLUTF8Encoder.encode(postBody);
here is the HttpRequest
httpConn = (HttpConnection) Connector.open(url
+
ConnectionUtils.getConnectionPerameter());
httpConn.setRequestMethod(HttpConnection.POST);
httpConn.setRequestProperty("WWW-Authenticate",
"OAuth realm=http://twitter.com/");
httpConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
httpConn.setRequestProperty("Authorization",
authHeader);
httpConn.setRequestProperty("Content-Length",Integer
.toString(postBody.getBytes().length));
os = httpConn.openOutputStream();
os.write(postBody.getBytes());
On Aug 18, 3:53 pm, "Ernandes Jr." <[email protected]> wrote:
> Hi,
>
> Check my xAuth implementation in TwAPIme,www.twapime.com. Maybe you can
> find what's wrong with your code.
>
> Regards,
> Ernandes
>
> On Wed, Aug 18, 2010 at 7:42 AM, Tom van der Woerdt <[email protected]> wrote:
>
>
>
> > The problem is that the last time I did Java (this is Java, right?) was
> > ages ago (I was 12 back then) so I don't really know what the code is
> > doing. The code looks fine to me, but like I said, I don't know Java
> > very well.
>
> > Can you show the actual request you make, instead of code? Like the
> > "POST /1/statuses/update.json HTTP/1.1" part. Also include the base
> > string which you use.
>
> > Tom
>
> > On 8/18/10 12:38 PM, LINUXGEEK wrote:
> > > Thanks for the replay i was waiting for your replay
>
> > > here is my post body
>
> > > String postBody = "status="
> > > + URLUTF8Encoder.encode("Test message");
>
> > > here is the base String
>
> > > String baseString = "POST&"
> > > + URLUTF8Encoder
> > > .encode("
> >http://api.twitter.com/1/statuses/update.json")
> > > + "&oauth_consumer_key%3D" + "my consumer
> > key"
> > > + "%26oauth_nonce%3D"+oauth_nonce+"a"
> > > +
> > "%26oauth_signature_method%3D"+oauth_signature_method
> > > + "%26oauth_token%3D"+auth_token
> > > + "%26oauth_timestamp%3D"+oauth_timestamp
> > > + "%26oauth_version%3D"+oauth_version +
> > "%26"
> > > + URLUTF8Encoder.encode(postBody);
>
> > > String signingSecret = URLUTF8Encoder.encode("my Secret key")+ "&"
> > +
> > > URLUTF8Encoder.encode("auth_secret");
>
> > > here is my signature
>
> > > String signature =
> > URLUTF8Encoder.encode(hmacsha1(signingSecret,
> > > baseString));
>
> > > here is my header
>
> > > String header = new StringBuffer("OAuth oauth_nonce=\"").append(
> > > oauth_nonce).append(
> > > "\", oauth_signature_method=\"").append(
> > > oauth_signature_method).append(
> > > "\", oauth_timestamp=\"").append(
> > > oauth_timestamp).append(
> > > "\", oauth_consumer_key=\"").append("my
> > consumer key")
> > > .append("\",oauth_token=\"").append(
> > > token.getToken()).append(
> > > "\",
> > oauth_signature=\"").append(
> > > signature).append(
> > > "\",
> > oauth_version=\"").append(oauth_version).append(
> > > "\"").toString();
>
> > > one more question here
> > > when i am making the http request do i need to write the post body to
> > > the outputStream..
>
> > > and tell me what should i change in the above code...
>
> > > On Aug 18, 3:28 pm, Tom van der Woerdt <[email protected]> wrote:
> > >> Hi,
>
> > >> That's a lot of code, but I prefer to know what it is doing over the
> > >> actual code. So: can you post a request which you make to Twitter, and
> > >> can you give the Base String which you use for generating the signature?
>
> > >> Tom
>
> > >> On 8/18/10 8:08 AM, LINUXGEEK wrote:
>
> > >>> private void StatusUpdate() throws CryptoTokenException,
> > >>> CryptoUnsupportedOperationException, IOException {
>
> > >>> String oauth_signature_method = "HMAC-SHA1";
> > >>> String oauth_timestamp = String.valueOf(timestamp());
> > >>> String oauth_nonce = createNonce(oauth_timestamp);
> > >>> String oauth_version = "1.0";
>
> > >>> String postBody = "status="
> > >>> + URLUTF8Encoder.encode("Finally i got
> > success");
>
> > >>> String baseString = "POST&"
> > >>> + URLUTF8Encoder
> > >>> .encode("
> >https://api.twitter.com/1/statuses/update.json")
> > >>> + "&oauth_consumer_key%3D" + "my consumer
> > key"
> > >>> + "%26oauth_nonce%3D" + oauth_nonce
> > >>> + "%26oauth_signature_method%3D" +
> > oauth_signature_method
> > >>> + "%26oauth_token%3D" + token.getToken()
> > >>> + "%26oauth_timestamp%3D" + oauth_timestamp
> > >>> + "%26oauth_version%3D" + oauth_version +
> > "%26"
> > >>> + URLUTF8Encoder.encode(postBody);
> > >>> String signingSecret = URLUTF8Encoder
> > >>> .encode("my consuer Secret")
> > >>> + "&" +
> > URLUTF8Encoder.encode(token.getSecret());
> > >>> String signature = hmacsha1(signingSecret, baseString);
> > >>> Dialog.alert(signature);
> > >>> String header = new StringBuffer("OAuth
> > oauth_nonce=\"").append(
> > >>> URLUTF8Encoder.encode(oauth_nonce)).append(
> > >>> "\", oauth_signature_method=\"").append(
>
> > URLUTF8Encoder.encode(oauth_signature_method)).append(
> > >>> "\", oauth_timestamp=\"").append(
>
> > URLUTF8Encoder.encode(oauth_timestamp)).append(
> > >>> "\", oauth_consumer_key=\"").append("my
> > consumer key")
> > >>> .append("\",oauth_token=\"").append(
>
> > URLUTF8Encoder.encode(token.getToken())).append(
> > >>> "\",
> > oauth_signature=\"").append(
>
> > URLUTF8Encoder.encode(signature)).append(
> > >>> "\",
> > oauth_version=\"").append(oauth_version).append(
> > >>> "\"").toString();
> > >>> UpdateMyNewStatus(header, postBody);
> > >>> }
>
> > >>> public static void UpdateMyNewStatus(String auth_header, String
> > body)
> > >>> {
> > >>> String url = "http://api.twitter.com/1/statuses/update.json
> > ";
> > >>> HttpConnection httpConn = null;
> > >>> InputStream input = null;
> > >>> OutputStream os = null;
> > >>> try {
>
> > >>> httpConn = (HttpConnection) Connector.open(url
> > >>> +
> > ConnectionUtils.getConnectionPerameter());
>
> > httpConn.setRequestMethod(HttpProtocolConstants.HTTP_METHOD_POST);
> > >>> httpConn.setRequestProperty("WWW-Authenticate",
> > >>> "OAuth realm=http://twitter.com/");
> > >>> httpConn.setRequestProperty("Content-Type",
>
> > "application/x-www-form-urlencoded");
> > >>> String authHeader = auth_header;
> > >>> httpConn.setRequestProperty("Authorization",
> > authHeader);
>
> > >>> // write post body
> > >>> String postBody = body;
> > >>> httpConn.setRequestProperty("Content-Length",
> > Integer
>
> > .toString(postBody.getBytes().length));
> > >>> os = httpConn.openOutputStream();
> > >>> os.write(postBody.getBytes());
> > >>> os.close();
> > >>> os = null;
>
> > >>> input = httpConn.openDataInputStream();
>
> > >>> int resp = httpConn.getResponseCode();
> > >>> Dialog.alert(httpConn.getResponseMessage());
>
> > >>> if (resp == HttpConnection.HTTP_OK) {
>
> > >>> StringBuffer buffer = new StringBuffer();
> > >>> int ch;
> > >>> while ((ch = input.read()) != -1) {
> > >>> buffer.append((char) ch);
> > >>> }
> > >>> String content = buffer.toString();
> > >>> Dialog.alert(content);
> > >>> }
>
> > >>> } catch (Exception e) {
> > >>> } finally {
> > >>> try {
> > >>> httpConn.close();
> > >>> input.close();
> > >>> } catch (IOException e) {
> > >>> e.printStackTrace();
> > >>> }
> > >>> }
> > >>> }
>
> > >>> i am using the above code..
>
> > >>> On Aug 17, 7:24 pm, Tom van der Woerdt <[email protected]> wrote:
> > >>>> On 8/17/10 1:47 PM, LINUXGEEK wrote:
>
> > >>>>> can any one help in updating the status of my twitter account by
> > using
> > >>>>> auth token and secret.
>
> > >>>>> am getting 401 error while doing this..
>
> > >>>>> can any one give me code for updating the status.
>
> > >>>> I could give you pseudocode, yes, but that will most likely be useless
> > >>>> to you.
>
> > >>>> Assuming that you know how to program a HTTP socket, what is the
> > request
> > >>>> you make to twitter before getting this 401 error, and what is the
> > base
> > >>>> string you use to generate the signature?
>
> > >>>> Tom
>
> > >>>> PS: People have said that I never mention the existence of libraries.
> > >>>> So, here I go:http://dev.twitter.com/pages/oauth_libraries
> > >>>> PPS: I dislike libraries.
>
> --
> Ernandes Jr.
> ---------------------
> "ALL programs are poems. However,
> NOT all programmers are poets."