[英]I keep getting the “OAuth consumer key/secret combination not supplied” error message
對於我的學校項目,我必須從Twitter收集數據。 現在,我找到了以下代碼,但是我一直收到OAuth使用方密鑰/秘密組合未提供的錯誤,而且我也不知道如何解決它。 有什么辦法嗎?
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import twitter4j.IDs;
import twitter4j.Paging;
import twitter4j.ResponseList;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.auth.AccessToken;
// TODO: preserve network distribution: normalize number of nodes
// TODO: discard re-tweet option
// TODO: multifocal approach
public class TwitterCrawler {
public static final String FOCAL_NODE = "euromast";
public static final int NUMBER_LINKS = 5;
public static final int NUMBER_NODES = 40;
public static final int NUMBER_STATUSES = 20;
public String networkOutputFile = "Users/Chris/Desktop/data/twitter.csv";
public String contentOutputFile = "/Users/Chris/Desktop/data/twitter2.txt";
private static Twitter twitter;
public TwitterCrawler() {
try {
twitter = new TwitterFactory().getInstance();
AccessToken token = twitter.getOAuthAccessToken();
System.out.println("Access Token " + token);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public HashSet<User> getFriends(User u, int max) throws Exception {
HashSet<User> ret = new HashSet<User>();
long cursor = -1;
IDs ids;
int count = 0;
do {
ids = twitter.getFriendsIDs(u.getScreenName(), cursor);
for (int i = 0; i < ids.getIDs().length && i < max; i++) {
long id = ids.getIDs()[i];
ret.add(twitter.showUser(id));
}
count++;
} while ((cursor = ids.getNextCursor()) != 0 && count < max);
return ret;
}
public void crawl() throws TwitterException, IOException {
ArrayList<User> ret = new ArrayList<User>();
HashMap<User, HashSet<Link>> links = new HashMap<User, HashSet<Link>>();
HashMap<User, ResponseList<Status>> statuses = new HashMap<User, ResponseList<Status>>();
String[] startnodes = { FOCAL_NODE };
Twitter twitter = new TwitterFactory().getInstance();
ResponseList<User> users = twitter.lookupUsers(startnodes);
// ret.addAll(users);
for (int i = 0; i < users.size() && ret.size() < NUMBER_NODES; i++) {
User user = (User) users.get(i);// it.next();
try {
// save links
Iterator it2 = this.getFriends(user, NUMBER_LINKS).iterator(); // this
// might
// throw
// an
// "unauthorized"
// exception
// that's
// why
// it
// should
// come
// first
links.put(user, new HashSet<Link>());
int countFriends = 0;
while (it2.hasNext() && countFriends < NUMBER_LINKS) {
User friend = (User) it2.next();
Link l = new Link(user, friend);
links.get(user).add(l);
users.add(friend);
countFriends++;
}
// successfully fetched user info, save user
ret.add(user);
// save statuses
Paging paging = new Paging(1, NUMBER_STATUSES);
ResponseList<Status> userStatuses = twitter.getUserTimeline(
user.getId(), paging);
statuses.put(user, userStatuses);
System.out.println("Done with: @" + user.getScreenName());
} catch (Exception e) {
// System.err.println(e.getMessage());
System.out.println("Error - not authorized for: "
+ user.getScreenName() + " - skipping...");
// remove links to people that had "unauthorized access"
// exception
for (HashSet<Link> linksPerUser : links.values()) {
ArrayList<Link> markedForRemoval = new ArrayList<Link>();
for (Link curLink : linksPerUser) {
if (curLink.from.getId() == user.getId()
|| curLink.to.getId() == user.getId()) {
markedForRemoval.add(curLink);
}
}
for (Link toRemoveLink : markedForRemoval) {
linksPerUser.remove(toRemoveLink);
}
}
}
}
saveToCSVFile(ret, links);
saveContentToFile(statuses);
}
/*
* Saves the network in NET format (Pajek)
* http://gephi.org/users/supported-graph-formats/pajek-net-format/
*/
public void saveToNetFile(ArrayList<User> users,
HashMap<User, HashSet<Link>> links,
HashMap<User, ResponseList<Status>> statuses) throws IOException {
FileOutputStream fout = new FileOutputStream(
new File(networkOutputFile));
PrintStream ps = new PrintStream(fout);
String linksString = "*Edges\n";
ps.println("*Vertices " + users.size());
for (User user : users) {
ps.println(user.getId() + " \"" + user.getScreenName() + "\"");
if (links.containsKey(user)) {
for (Link link : links.get(user)) {
linksString += link.toString() + "\n";
}
}
}
ps.println(linksString);
ps.close();
fout.close();
}
public void saveToCSVFile(ArrayList<User> users,
HashMap<User, HashSet<Link>> links) throws IOException {
FileOutputStream fout = new FileOutputStream(
new File(networkOutputFile));
PrintStream ps = new PrintStream(fout);
for (HashSet<Link> linksPerUser : links.values()) {
if (linksPerUser.size() > 0) {
ps.print(linksPerUser.iterator().next().from.getScreenName()
+ ";");
for (Link curLink : linksPerUser) {
ps.print(curLink.to.getScreenName() + ";");
}
ps.print("\n");
}
}
ps.close();
fout.close();
}
public void saveContentToFile(HashMap<User, ResponseList<Status>> statuses)
throws IOException {
FileOutputStream fout = new FileOutputStream(
new File(contentOutputFile));
PrintStream ps = new PrintStream(fout);
for (ResponseList<Status> statusesPerUser : statuses.values()) {
if (statusesPerUser.size() > 0) {
ps.println(statusesPerUser.iterator().next().getUser()
.getScreenName()
+ " " + statusesPerUser.size());
for (Status curStatus : statusesPerUser) {
ps.println(curStatus.getText().replace("\n", "") + ""); // very
// important:
// replace
// line
// breaks
}
}
}
ps.close();
fout.close();
}
public static void main(String[] args) {
try {
TwitterCrawler cn = new TwitterCrawler();
cn.crawl();
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to lookup users: " + te.getMessage());
System.exit(-1);
} catch (Exception e) {
e.printStackTrace();
}
}
class Link {
public User from;
public User to;
public Link(User f, User t) {
from = f;
to = t;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || !(this.getClass() != obj.getClass())) {
return false;
}
Link other = (Link) obj;
return this.from.getId() == other.from.getId()
&& this.to.getId() == other.to.getId();
}
@Override
public int hashCode() {
return String.valueOf(this.from.getId()).hashCode()
+ String.valueOf(this.to.getId()).hashCode();
}
@Override
public String toString() {
return String.valueOf(this.from.getId()) + " "
+ String.valueOf(this.to.getId());
}
}
}
我必須承認,自上次創建Twitter應用程序已經過去兩年了。 但是我記得認證過程很復雜而且很繁瑣。 我在您的代碼中找不到任何身份驗證步驟,因此我想您的問題是由該事實引起的。 您有很多選擇方式來驗證您的搜尋器。 但據我所知,您只希望訪問用戶的好友列表。
對於此用例,您僅需要單用戶身份驗證 。 為了獲得一個,請遵循本頁上的步驟。
請注意,您可以提出的請求數量會受到限制。
只是想一想,我相信Eclipse要求用於某些應用程序的文件必須位於src文件中。 例如,它可以在項目中,而不是.txt文件的src,但是對於圖像,它必須在src文件夾中。
我不熟悉該錯誤,但是可以解決。 使用Eclipse制作帶有圖像的簡單GUI時出現了奇怪的錯誤。 然后我意識到它需要放在src文件夾中,而不僅僅是項目文件夾中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.