Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 17
Registered: ‎08-04-2010
My Device: Not Specified

Twitter status updation error - unauthorized error (401)

Hi

 

I am using your status updation code which I got from blackberry forum

 

Code

public static void StatusUpdate() throws CryptoTokenException,CryptoUnsupportedOperationException, IOException

      {

            String oauth_signature_method = "HMAC-SHA1";

          String oauth_timestamp = String.valueOf(timestamp());

          String oauth_nonce = nonce();

            String oauth_version = "1.0";

 

          String postBody = "status="+ URLUTF8Encoder.encode("a");

          String baseString = "POST&"

                        + URLUTF8Encoder.encode("http://api.twitter.com/1/statuses/update.json")                  

                        + "&oauth_consumer_key%3D" + Const.consumerKey

                              + "%26oauth_nonce%3D" + oauth_nonce                        

                      + "%26oauth_signature_method%3D" + oauth_signature_method

                      + "%26oauth_timestamp%3D" + oauth_timestamp             

                      + "%26oauth_token%3D" + token[0]

                              + "%26oauth_version%3D" + oauth_version + "%26"

                              + URLUTF8Encoder.encode(postBody);

 

            String signingSecret = URLUTF8Encoder.encode(Const.consumerSecret)+ "&" + URLUTF8Encoder.encode(token[1]);

          String signature = hmacsha1(signingSecret, baseString);

 

         // Dialog.alert(signature);

//          String new_url = "https://api.twitter.com/1/statuses/update.json";

//        String header_1 = oauth_header(new_url, HttpProtocolConstants.HTTP_METHOD_POST);

 

          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(URLUTF8Encoder.encode(Const.consumerKey))

                              .append("\", oauth_token=\"").append(URLUTF8Encoder.encode(token[0]))

                              .append("\", oauth_signature=\"").append(URLUTF8Encoder.encode(signature))

                              .append("\", oauth_version=\"").append(oauth_version).append("\"").toString();

//       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(Const.consumerKey)

//       .append("\", oauth_token=\"").append(token[0])

//       .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); // kris connection

 

                        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);

                  System.out.println(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();

                 }

            }

 

But I am getting unauthorized error (401)… I think u have solved the issue .

I have changed the baseString parameter .but no use …can you pls tell me what all changes you made to work this code.

 

Regards

kris

SalmanFaris
Contributor
Posts: 17
Registered: ‎08-04-2010
My Device: Not Specified

Re: Twitter status updation error - unauthorized error (401)

Hi

 

this is my baseString

 

POST&https%3A%2F%2Fapi.twitter.com%2F1%2Fstatuses%2Fupdate.json&oauth_consumer_key%3DsIbZByaOIjI0cJ6u8C3Q%26oauth_nonce%3D2980982221033147680%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1289974129%26oauth_token%3D111232583-3oi9EdbaxtaoIASxneqi3IjYlhhRKe8rHyVPmk%26oauth_version%3D1.0%26status%3Da 

 

Authorization Header

 

OAuth realm="",oauth_version="1.0",oauth_consumer_key="sIbZByaOIjI0cJ6u8C3Q",oauth_token="111232583-3oi9EdbaxtaoIASxneqi3IjYlhhRKe8rHyVPmk",oauth_timestamp="1289974129",oauth_nonce="2980982221033147680",oauth_signature_method="HMAC-SHA1",oauth_signature="QYSL64kXJWUgh7hWFrvw208Ekxg%3D"

 

am i doing correct ...still i am getting unauthorized error...401

pls help me 

 

Regards 

Kris

 

SalmanFaris
Highlighted
Contributor
Posts: 17
Registered: ‎08-04-2010
My Device: Not Specified

Re: Twitter status updation error - unauthorized error (401)

Hi 

 

got solved ..the problem was in the application...it was read only...i changed it ...nw its wrking like charm..

 

Regards 

kris

SalmanFaris
Developer
Posts: 528
Registered: ‎10-06-2009
My Device: 9800,BB Dev Alpha,z10 limited edition
My Carrier: Vodafone

Re: Twitter status updation error - unauthorized error (401)

Hi,

I also have put the application as read & write but nothing changed.Can you please help me?

Rujuta Trivedi