srss.core.TweetPlugin.java Source code

Java tutorial

Introduction

Here is the source code for srss.core.TweetPlugin.java

Source

package srss.core;

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;

/**[Module TweetPlugin.java]
 Copyright(c) 2015 mrtska.starring
 This software is released under the MIT License.
 http://opensource.org/licenses/mit-license.php
 Created on: 2015/03/23
*/
public class TweetPlugin {

    //
    private Twitter twitter;

    //?????
    private RequestToken reqest;

    private final Config conf;

    public TweetPlugin(Config conf) throws TwitterException {

        this.conf = conf;

        String accessTokenString = conf.getAccessToken();

        if (!accessTokenString.equals("null")) {

            TwitterFactory factory = new TwitterFactory();
            AccessToken token = new AccessToken(conf.getAccessToken(), conf.getAccessTokenSecret());

            this.twitter = factory.getInstance();
            this.twitter.setOAuthAccessToken(token);
        }
    }

    //PIN???
    public void authReqest() {

        try {
            this.twitter = TwitterFactory.getSingleton();
            this.reqest = twitter.getOAuthRequestToken();
            Desktop.getDesktop().browse(new URI(this.reqest.getAuthorizationURL()));

        } catch (TwitterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    public int auth(String pin) {

        try {
            AccessToken token = twitter.getOAuthAccessToken(reqest, pin);
            conf.setAccessToken(token.getToken());
            conf.setAccessTokenSecret(token.getTokenSecret());

            return 0;
        } catch (TwitterException e) {

            return -1;
        }
    }

    public Twitter getTwitterInstance() {

        return this.twitter;
    }

}