title: How to use Twitter Rest APi with OAuth
body: <p>With the help of <a href="https://stackoverflow.com/questions/13387025/simplest-java-example-retrieving-user-timeline-with-twitter-api-version-1-1?lq=1">Simplest Java example retrieving user_timeline with twitter API version 1.1</a> I am trying to retrieve user twiter messages. But facing three problems. I need to use android 2.3 Please help if you can</p>
  
  <pre><code>    public void testUserTimelineWithParams() throws Exception {
      //This will read the timeline of the 'twitterapi' account.
  
      String method = "GET";
      String url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
      List&lt;NameValuePair&gt; urlParams = new ArrayList&lt;NameValuePair&gt;();
      urlParams.add( new NameValuePair("screen_name","twitterapi") );
      urlParams.add( new NameValuePair("count", "10") );
  
      String oAuthConsumerKey = "Your value";
      String oAuthConsumerSecret = "Your value"; //&lt;--- DO NOT SHARE THIS VALUE
  
      String oAuthAccessToken = "Your value";
      String oAuthAccessTokenSecret = "Your value"; //&lt;--DO NOT SHARE THIS VALUE
  
      String oAuthNonce = String.valueOf(System.currentTimeMillis());
      String oAuthSignatureMethod = "HMAC-SHA1";
      String oAuthTimestamp = time();
      String oAuthVersion = "1.0";
  
      String signatureBaseString1 = method;
      String signatureBaseString2 = url;
  
      List&lt;NameValuePair&gt; allParams = new ArrayList&lt;NameValuePair&gt;();
  
      allParams.add(new NameValuePair("oauth_consumer_key", oAuthConsumerKey));
      allParams.add(new NameValuePair("oauth_nonce", oAuthNonce));
      allParams.add(new NameValuePair("oauth_signature_method", oAuthSignatureMethod));
      allParams.add(new NameValuePair("oauth_timestamp", oAuthTimestamp));
      allParams.add(new NameValuePair("oauth_token", oAuthAccessToken));
      allParams.add(new NameValuePair("oauth_version", oAuthVersion));
      allParams.addAll(urlParams);
  
      Collections.sort(allParams, new NvpComparator());
  
      StringBuffer signatureBaseString3 = new StringBuffer();
      for(int i=0;i&lt;allParams.size();i++)
      {
          NameValuePair nvp = allParams.get(i);
          if (i&gt;0) {
              signatureBaseString3.append("&amp;");
          }
          signatureBaseString3.append(nvp.getName() + "=" + nvp.getValue());
      }
  
      String signatureBaseStringTemplate = "%s&amp;%s&amp;%s";
      String signatureBaseString =  String.format(signatureBaseStringTemplate, 
                                                                         URLEncoder.encode(signatureBaseString1, "UTF-8"), 
                                                                  URLEncoder.encode(signatureBaseString2, "UTF-8"),
                                                                  URLEncoder.encode(signatureBaseString3.toString(), "UTF-8"));
  
  
      System.out.println("signatureBaseString: "+signatureBaseString);
  
      String compositeKey = URLEncoder.encode(oAuthConsumerSecret, "UTF-8") + "&amp;" + URLEncoder.encode(oAuthAccessTokenSecret, "UTF-8");
  
      String oAuthSignature =  computeSignature(signatureBaseString, compositeKey);
      System.out.println("oAuthSignature       : "+oAuthSignature);
  
      String oAuthSignatureEncoded = URLEncoder.encode(oAuthSignature, "UTF-8");
      System.out.println("oAuthSignatureEncoded: "+oAuthSignatureEncoded);
  
      String authorizationHeaderValueTempl = "OAuth oauth_consumer_key=\"%s\", oauth_nonce=\"%s\", oauth_signature=\"%s\", oauth_signature_method=\"%s\", oauth_timestamp=\"%s\", oauth_token=\"%s\", oauth_version=\"%s\"";
  
      String authorizationHeaderValue = String.format(authorizationHeaderValueTempl,
                                                          oAuthConsumerKey,
                                                          oAuthNonce,
                                                          oAuthSignatureEncoded,
                                                          oAuthSignatureMethod,
                                                          oAuthTimestamp,
                                                          oAuthAccessToken,
                                                          oAuthVersion);
      System.out.println("authorizationHeaderValue: "+authorizationHeaderValue);
  
      StringBuffer urlWithParams = new StringBuffer(url);
      for(int i=0;i&lt;urlParams.size();i++) {
          if(i==0) 
          {
              urlWithParams.append("?");
          }
          else
          {
              urlWithParams.append("&amp;");
          }
          NameValuePair urlParam = urlParams.get(i);
          urlWithParams.append(urlParam.getName() + "=" + urlParam.getValue());
      }
  
      System.out.println("urlWithParams: "+urlWithParams.toString());
      System.out.println("authorizationHeaderValue:"+authorizationHeaderValue);
  
      GetMethod getMethod = new GetMethod(urlWithParams.toString());
      getMethod.addRequestHeader("Authorization", authorizationHeaderValue);
  
      HttpClient cli = new HttpClient();
      int status = cli.executeMethod(getMethod);
      System.out.println("Status:"+status);
  
      long responseContentLength = getMethod.getResponseContentLength();
      System.out.println("responseContentLength:"+responseContentLength);
  
      String response = getMethod.getResponseBodyAsString();  
      System.out.println("response: "+response);
  }   
  </code></pre>
  
  <p>Errors:</p>
  
  <pre><code>**Cannot instantiate the type NameValuePair**
  at line allParams.add(new NameValuePair("oauth_consumer_key", oAuthConsumerKey)); 
  
  **- GetMethod cannot be resolved to a type**
  at Line GetMethod getMethod = new GetMethod(urlWithParams.toString());
  
  **- GetMethod cannot be resolved to a type**
  at Line HttpClient cli = new HttpClient();
  
  **The method encodeBase64(byte[]) is undefined for the type Base64**
  at Line return new String(Base64.encodeBase64(mac.doFinal(text))).trim();
  </code></pre>