简体   繁体   English

我不断收到“未提供OAuth使用方密钥/秘密组合”错误消息

[英]I keep getting the “OAuth consumer key/secret combination not supplied” error message

for my school project I got to collect data from twitter. 对于我的学校项目,我必须从Twitter收集数据。 Now I have found the following code, but I keep getting the OAuth consumer key/secret combination not supplied error, and I have no clue how to fix it. 现在,我找到了以下代码,但是我一直收到OAuth使用方密钥/秘密组合未提供的错误,而且我也不知道如何解决它。 Any solutions? 有什么办法吗?

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());
    }
}

} }

I have to admit it's been two years since I last created a twitter application. 我必须承认,自上次创建Twitter应用程序已经过去两年了。 But I remember that the authentication process is complex and rather tedious. 但是我记得认证过程很复杂而且很繁琐。 I can' find any authentication steps in your code so I guess that your problem stems from that fact. 我在您的代码中找不到任何身份验证步骤,因此我想您的问题是由该事实引起的。 You have a lot of choices how you want to authenticate your crawler. 您有很多选择方式来验证您的搜寻器。 But as far as I can see you only want access friendlists of users. 但据我所知,您只希望访问用户的好友列表。

For this use case you only need a single user authentication . 对于此用例,您仅需要单用户身份验证 In order to obtain one follow the steps on this page . 为了获得一个,请遵循本页上的步骤。

Please note that there will be a restraint in the number of requests you can make. 请注意,您可以提出的请求数量会受到限制。

Just a thought, I believe that Eclipse requires the files being used to be located inside the src file for certain applications. 只是想一想,我相信Eclipse要求用于某些应用程序的文件必须位于src文件中。 For example, it can be in the project and not the src for .txt files, but for images, it must be in the src folder. 例如,它可以在项目中,而不是.txt文件的src,但是对于图像,它必须在src文件夹中。

I'm not familiar with that error, but it could be a solution. 我不熟悉该错误,但是可以解决。 I got weird errors with eclipse making simple GUIs with images. 使用Eclipse制作带有图像的简单GUI时出现了奇怪的错误。 Then I realized it needed to be in the src folder, and not just the project folder. 然后我意识到它需要放在src文件夹中,而不仅仅是项目文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM