2014-03-12 2 views
0

스트리밍 API를 사용하여 트위터에서 데이터를 가져 오려고하고 있는데 기본 인증을 사용하고 있습니다. 하지만 기본적으로 잘못된 사용자 이름이나 암호에 대한 401 오류가 나타납니다. 아래는 제 코드입니다. 또한 콘솔 오류가 발생했습니다. 어느 누구라도 제가하고있는 실수를 말해주십시오. 콘솔트위터에서 데이터를 가져올 수 없음

코드

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와 같은 URL을 변경했지만 여전히 동일한 오류가 발생합니다. 어떤 작업 arround 제안하십시오. 고마워요! – priyaranjan

+0

아니요 , 기본 인증은 더 이상 지원되지 않습니다. 파이썬의 경우 https://code.google.com/p/python-twitter/ tweepy와 같은 라이브러리로 시작할 수 있습니다. 저는 simplegeo에서 파이썬 oauth 라이브러리를 사용하고 더 많은 컨트롤을하지만 파이썬 트위터로 시작하는 것은 처음에 빨리 시작하고 세부 사항을 이해하는 데 도움이되었습니다. – Vishal

답변

1

트위터 APIv1.0은 은퇴되었습니다. this을 확인하십시오.

트위터가 기본 인증을 더 이상 지원하지 않습니다. this을 확인하십시오.

oauth를 지원하는 twitter 라이브러리로 시작하는 python의 경우 블록을 빠르게 벗어날 수 있습니다. python-twittertweepy은 몇 가지 stackoverflow 지지자와 함께 상당히 널리 사용되는 것처럼 보입니다.

처음에는 python-twitter를 사용했고 더 많은 제어를 위해 simplegeo's oauth 라이브러리를 사용하여 Twitter1.1 API를 직접 호출하기 전에 세부 사항을 빨리 이해하고 이해하는 데 도움이되었습니다.

+0

도움을 주신 Vishal에게 감사드립니다. 이제 oauth를 사용하여 트위터에 연결할 수 있습니다. twiteer4j를 사용하고 있습니다 :) 감사합니다 !! – priyaranjan

관련 문제