无法从Twitter获取数据

时间:2014-03-12 11:25:06

标签: java twitter

我正在尝试使用流API从twitter获取数据,而我正在使用基本身份验证。但我得到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 = "priya.XXXXX@gmail.com: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

1 个答案:

答案 0 :(得分:1)

Twitter APIv1.0已经停用。检查this

Twitter不再支持Basic Auth。检查this

对于从支持oauth的twitter库开始的python会让你快速离开块。 python-twittertweepy似乎与几个stackoverflow支持者相当广泛使用。

我最初使用了python-twitter,它帮助快速入门并了解详细信息,然后使用simplegeo's oauth库直接调用Twitter1.1 API以获得更多控制权。