401:身份验证凭据无效 - 令牌无效或过期。代码 - 89

时间:2018-04-22 20:50:01

标签: twitter twitter4j

这是代码,我收到错误401:身份验证错误


    public class Server {
        // initialize socket and input stream
        private Socket socket = null;
        private ServerSocket server = null;
        private DataInputStream in = null;

        public void tweet() throws TwitterException {
            ConfigurationBuilder cb = new ConfigurationBuilder();
            cb.setDaemonEnabled(true).setOAuthConsumerKey("......")
                    .setOAuthConsumerSecret("......")
                    .setOAuthAccessToken("......")
                    .setOAuthAccessTokenSecret(".....");

            TwitterFactory tf = new TwitterFactory();
            twitter4j.Twitter twitter = tf.getInstance();
            List status = twitter.getHomeTimeline();
            for (Status st : status) {
                System.out.println(st.getUser().getName() + "---- Tweets----" + st.getText());
            }

        }

        // constructor with port
        public Server(int port) throws TwitterException {
            // starts server and waits for a connection
            try {
                server = new ServerSocket(port);
                System.out.println("Server started");

                System.out.println("Waiting for a client ...");

                socket = server.accept();
                System.out.println("Client accepted");

                // takes input from the client socket
                in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));

                String line = "";

                // reads message from client until "Over" is sent
                while (!line.equals("Over")) {
                    try {
                        line = in.readUTF();
                        System.out.println(line);
                        if (line.equalsIgnoreCase("Data")) {
                            tweet();
                        }

                    } catch (IOException i) {
                        System.out.println(i);
                    }
                }
                System.out.println("Closing connection");

                // close connection
                socket.close();
                in.close();
            } catch (IOException i) {
                System.out.println(i);
            }
        }

        public static void main(String args[]) throws TwitterException {
            Server server = new Server(5000);
        }
    }

1 个答案:

答案 0 :(得分:0)

请确保令牌有效。

然后,您可以尝试启用系统代理,如下所示:

System.setProperty("java.net.useSystemProxies", "true");