twitter4j – update status例子(Java)

來源:互聯網
上載者:User
原文:https://github.com/yusuke/twitter4j/blob/master/twitter4j-examples/src/main/java/twitter4j/examples/tweets/UpdateStatus.java/* * Copyright 2007 Yusuke Yamamoto * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package twitter4j.examples.tweets;import twitter4j.Status;import twitter4j.Twitter;import twitter4j.TwitterException;import twitter4j.TwitterFactory;import twitter4j.auth.AccessToken;import twitter4j.auth.RequestToken;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;/** * Example application that uses OAuth method to acquire access to your account.<br> * This application illustrates how to use OAuth method with Twitter4J.<br> * * @author Yusuke Yamamoto - yusuke at mac.com */public final class UpdateStatus {    /**     * Usage: java twitter4j.examples.tweets.UpdateStatus [text]     *     * @param args message     */    public static void main(String[] args) {        if (args.length < 1) {            System.out.println("Usage: java twitter4j.examples.tweets.UpdateStatus [text]");            System.exit(-1);        }        try {            Twitter twitter = new TwitterFactory().getInstance();            try {                // get request token.                // this will throw IllegalStateException if access token is already available                RequestToken requestToken = twitter.getOAuthRequestToken();                System.out.println("Got request token.");                System.out.println("Request token: " + requestToken.getToken());                System.out.println("Request token secret: " + requestToken.getTokenSecret());                AccessToken accessToken = null;                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));                while (null == accessToken) {                    System.out.println("Open the following URL and grant access to your account:");                    System.out.println(requestToken.getAuthorizationURL());                    System.out.print("Enter the PIN(if available) and hit enter after you granted access.[PIN]:");                    String pin = br.readLine();                    try {                        if (pin.length() > 0) {                            accessToken = twitter.getOAuthAccessToken(requestToken, pin);                        } else {                            accessToken = twitter.getOAuthAccessToken(requestToken);                        }                    } catch (TwitterException te) {                        if (401 == te.getStatusCode()) {                            System.out.println("Unable to get the access token.");                        } else {                            te.printStackTrace();                        }                    }                }                System.out.println("Got access token.");                System.out.println("Access token: " + accessToken.getToken());                System.out.println("Access token secret: " + accessToken.getTokenSecret());            } catch (IllegalStateException ie) {                // access token is already available, or consumer key/secret is not set.                if (!twitter.getAuthorization().isEnabled()) {                    System.out.println("OAuth consumer key/secret is not set.");                    System.exit(-1);                }            }            Status status = twitter.updateStatus(args[0]);            System.out.println("Successfully updated the status to [" + status.getText() + "].");            System.exit(0);        } catch (TwitterException te) {            te.printStackTrace();            System.out.println("Failed to get timeline: " + te.getMessage());            System.exit(-1);        } catch (IOException ioe) {            ioe.printStackTrace();            System.out.println("Failed to read the system input.");            System.exit(-1);        }    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.