我想从使用流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
Twitter APIv1.0已经停用(https://dev.twitter.com/docs/api/1)。基本身份验证也不支持。检查:https://dev.twitter.com/docs/deprecations/spring-2012 – Vishal
感谢Vishal的回复,我是twiteer API的新手,请问我是否支持API 1.1基本身份验证? 。我只是更改了网址,例如https://stream.twitter.com/1.1/statuses/filter.json?track="+Tokey,但我仍然得到同样的错误。请建议任何工作周围。非常感谢! – priyaranjan
否,基本的身份验证不再支持,对于python,你可以从像https://code.google.com/p/python-twitter/或tweepy这样的库开始,我使用simplegeo的python oauth库并直接调用API更多的控制权,但从python-twitter开始帮助最初开始快速了解细节。 – Vishal