2013-04-26 153 views
-2

Simplest Java example retrieving user_timeline with twitter API version 1.1的帮助下,我试图检索用户提示信息。但面临三个问题。我需要使用Android 2.3请帮助,如果你能如何使用Twitter Rest APi与OAuth

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<NameValuePair> urlParams = new ArrayList<NameValuePair>(); 
    urlParams.add(new NameValuePair("screen_name","twitterapi")); 
    urlParams.add(new NameValuePair("count", "10")); 

    String oAuthConsumerKey = "Your value"; 
    String oAuthConsumerSecret = "Your value"; //<--- DO NOT SHARE THIS VALUE 

    String oAuthAccessToken = "Your value"; 
    String oAuthAccessTokenSecret = "Your value"; //<--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<NameValuePair> allParams = new ArrayList<NameValuePair>(); 

    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<allParams.size();i++) 
    { 
     NameValuePair nvp = allParams.get(i); 
     if (i>0) { 
      signatureBaseString3.append("&"); 
     } 
     signatureBaseString3.append(nvp.getName() + "=" + nvp.getValue()); 
    } 

    String signatureBaseStringTemplate = "%s&%s&%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") + "&" + 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<urlParams.size();i++) { 
     if(i==0) 
     { 
      urlWithParams.append("?"); 
     } 
     else 
     { 
      urlWithParams.append("&"); 
     } 
     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); 
} 

错误:

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

回答

0

保持简单:使用twitter4j让你的要求,你不需要编写自己的;)

+0

感谢您的好方向 – rmimran 2013-04-26 10:42:21