title: Twitter Oauth Failed due to signature and token
body: <p>I am trying to crate a application to authenticate from twitter but am getting the following error
  :</p>
  
  <pre><code>Failed to validate oauth signature and token
  </code></pre>
  
  <p>Here is my code for app:</p>
  
  <pre><code>public class TwitCons {
  
      String sign_method ="HMAC-SHA1";
  
      public static String consumer_key ="^^^^^^^^^^^^^^^^^^^^";
      public static String consumer_secret="^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^";
      String request_uri = "https://api.twitter.com/oauth/request_token";
      String oauth_callback="http://127.0.0.1:8080/twitter/getToken";
  
      String getNonce(){
          Random ran = new Random(); 
          long nonce1 = ran.nextLong(); 
  
          Long nonce = Math.abs(new Long(nonce1)); 
  
          return nonce.toString();
      }
  
  
      String getTimestamp(){
          long time = System.currentTimeMillis();
  
  
          return String.valueOf(time/1000);
      }
  
  
      String getSignature(String request_uri ,String request_token,String nonce) throws UnsupportedEncodingException
      {
          String base="oauth_callback=http://127.0.0.1:8080/twitter/getToken&amp;oauth_consumer_key="+consumer_key+"&amp;oauth_nonce="+nonce+"&amp;oauth_signature_method="+sign_method+"&amp;oauth_timestamp="+getTimestamp()+"&amp;oauth_token=&amp;oauth_version=1.0";
  
          String baseString="POST&amp;"+URLEncoder.encode(request_uri , "UTF-8")+"&amp;"+URLEncoder.encode(base , "UTF-8");
  
          String signKey = consumer_secret +"&amp;"+ request_token ;
          System.out.println(signKey +"\n"+baseString);
  
          try {
              SecretKeySpec signingKey = new SecretKeySpec(signKey.getBytes(),"HmacSHA1");
              Mac mac = Mac.getInstance(signingKey.getAlgorithm());
              mac.init(signingKey);
              byte[] rawHmac = mac.doFinal(baseString.getBytes());
              String result = new String(Base64.encodeBase64(rawHmac));
              System.out.println(URLEncoder.encode(result , "UTF-8"));
              return URLEncoder.encode(result , "UTF-8");
              //return result;
  
          } catch (GeneralSecurityException e) {
              return "";
          }
  
  
  
  
      }
  
  
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public class Twitter extends TwitCons{
  
      public void getRequest() throws IllegalArgumentException, HttpException, IOException
      {
  
          HttpClient client = new HttpClient();
          PostMethod post = new PostMethod("https://api.twitter.com/oauth/request_token");
          String nonce=getNonce();
          String header = "oauth_callback=\""+URLEncoder.encode(oauth_callback ,"UTF-8")+"\","+
              "oauth_consumer_key=\""+consumer_key +"\","+
              "oauth_nonce=\""+nonce + "\","+
              "oauth_signature=\""+getSignature(request_uri,"",nonce) + "\","+ 
              "oauth_signature_method=\""+sign_method + "\","+
              "oauth_timestamp=\""+getTimestamp() + "\","+
              "oauth_version=\"1.0\""  ;
  
          post.addRequestHeader("Authorization", "OAuth "+header);
          client.executeMethod(post);
  
          System.out.println(post.getResponseBodyAsString()+"\n"+post.getRequestHeader("Authorization"));
  
  
  
  
  
  
      }
  </code></pre>
  
  <p>Class has method getRequest for making the twitter request for getting request_token and token_secret</p>
  
  <p>and second class TwitCons have different methods and constants to get request token</p>