2014-03-12 127 views
0

我想從使用流API的twitter獲取數據,並且我正在使用基本身份驗證。但我得到401錯誤,這基本上是錯誤的用戶名或密碼。以下是我的代碼。此外,我已經給出了我得到控制檯的錯誤。請問任何人請告訴我我在做什麼錯誤。無法從twitter獲取數據

代碼

package org.hi.hello; 
import java.io.*; 
import java.net.ConnectException; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 
import java.util.Map; 

import org.apache.commons.codec.binary.Base64; 

public class TwitterGetter { 

    public static void getTweetStream(String Tokey) throws InterruptedException, IOException{ 


     URL tweetCon = new URL("https://stream.twitter.com/1/statuses/filter.json?track="+Tokey); 
     URLConnection openTweetCon = tweetCon.openConnection(); 
     String login = "[email protected]:XXX123"; 
     String encoding = new String(Base64.encodeBase64(login.getBytes())) ;//((login.getBytes())); 
     openTweetCon.setRequestProperty("Authorization", encoding); 
     BufferedReader in = new BufferedReader(new InputStreamReader(openTweetCon.getInputStream())); 

     String inputLine; 




     while ((inputLine = in.readLine()) != null) 
     { 
      System.out.println(inputLine); 
     } 


      //basicDbObject.put("text", JSONObject.fromString(inputLine).get("text")); 
      //coll.save(basicDbObject); 

    } 



    public static void main(String[] args) throws IOException, InterruptedException { 
     Boolean Cont = true; 
     cont: 
     do { 
      try 
      { 

     getTweetStream("Trip,trip,games,futball,amex,creditcard,citibank,mastercard,vacation,electronics,train,americanexpress,platinumcard,visacredit"); 
      }catch (IOException e){System.err.println("Connection Error"); System.out.println(e.getMessage()); 
      System.out.println(e.getStackTrace());} 
      catch (Exception e){System.err.println("Encountered Exception: Restarting"); continue cont;}}while (Cont==true);} 


    } 

錯誤控制檯

Connection Error 
Server returned HTTP response code: 401 for URL: https://stream.twitter.com/1/statuses/filter.json?track=Trip,trip,games,futball,amex,creditcard,citibank,mastercard,vacation,electronics,train,americanexpress,platinumcard,visacredit 
[Ljava.lang.StackTraceElement;@1089cc5e 
Connection Error 
Server returned HTTP response code: 401 for URL: https://stream.twitter.com/1/statuses/filter.json?track=Trip,trip,games,futball,amex,creditcard,citibank,mastercard,vacation,electronics,train,americanexpress,platinumcard,visacredit 
[Ljava.lang.StackTraceElement;@3d0bbf9e 
Connection Error 
Server returned HTTP response code: 401 for URL: https://stream.twitter.com/1/statuses/filter.json?track=Trip,trip,games,futball,amex,creditcard,citibank,mastercard,vacation,electronics,train,americanexpress,platinumcard,visacredit 
[Ljava.lang.StackTraceElement;@77ce3fc5 
Connection Error 
Server returned HTTP response code: 401 for URL: https://stream.twitter.com/1/statuses/filter.json?track=Trip,trip,games,futball,amex,creditcard,citibank,mastercard,vacation,electronics,train,americanexpress,platinumcard,visacredit 
[Ljava.lang.StackTraceElement;@5fe0f2f6 
+0

Twitter APIv1.0已經停用(https://dev.twitter.com/docs/api/1)。基本身份驗證也不支持。檢查:https://dev.twitter.com/docs/deprecations/spring-2012 – Vishal

+0

感謝Vishal的回覆,我是twiteer API的新手,請問我是否支持API 1.1基本身份驗證? 。我只是更改了網址,例如https://stream.twitter.com/1.1/statuses/filter.json?track="+Tokey,但我仍然得到同樣的錯誤。請建議任何工作周圍。非常感謝! – priyaranjan

+0

否,基本的身份驗證不再支持,對於python,你可以從像https://code.google.com/p/python-twitter/或tweepy這樣的庫開始,我使用simplegeo的python oauth庫並直接調用API更多的控制權,但從python-twitter開始幫助最初開始快速瞭解細節。 – Vishal

回答

1

Twitter的APIv1.0已經退役。檢查this

Twitter不再支持基本身份驗證。檢查this

對於從支持oauth的twitter庫開始的python會讓你快速脫離塊。 python-twittertweepy似乎相當廣泛地與幾個疊加支持者一起使用。

我最初使用了python-twitter,它幫助您快速入門並瞭解細節,然後再使用simplegeo's oauth庫直接調用Twitter1.1 API以獲得更多控制權。

+0

感謝Vishal的幫助,現在我可以使用oauth連接到twitter,我正在使用twiteer4j :)謝謝! – priyaranjan