我想從使用流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