jp.xxxxxxxx.l3fish.twnyaan.service.AuthenticationService.java Source code

Java tutorial

Introduction

Here is the source code for jp.xxxxxxxx.l3fish.twnyaan.service.AuthenticationService.java

Source

/*
 * The MIT License
 *
 * Copyright 2016 sashimi.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package jp.xxxxxxxx.l3fish.twnyaan.service;

import com.google.inject.Singleton;
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import jp.xxxxxxxx.l3fish.twnyaan.config.ErrorCode;
import jp.xxxxxxxx.l3fish.twnyaan.config.TwitterAPIKey;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import twitter4j.conf.ConfigurationBuilder;

/**
 * Twitter???
 *
 * @author sashimi
 */
@Singleton
public class AuthenticationService {

    /**
     * RequestToken????URL???????PIN???
     * RequestToken??????{@code null}?
     *
     * @return ???RequestToken??????????{@code null}
     */
    public RequestToken authorize() {
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true).setOAuthConsumerKey(TwitterAPIKey.getConsumerKey())
                .setOAuthConsumerSecret(TwitterAPIKey.getConsumerSecret());
        Twitter twitter = new TwitterFactory(cb.build()).getInstance();

        RequestToken requestToken = null;
        try {
            requestToken = twitter.getOAuthRequestToken();
            URI authorizationURI = new URI(requestToken.getAuthorizationURL());
            Desktop desktop = Desktop.getDesktop();
            desktop.browse(authorizationURI);
        } catch (URISyntaxException | IOException e) {
            System.err.println(ErrorCode.CANNOT_OPEN_AUTHORIZATION_URI);
        } catch (TwitterException e) {
            System.err.println(ErrorCode.TWITTER_SERVICE_UNAVAILABLE);
        }

        return requestToken;
    }

    /**
     * RequestToken?PIN??AccessToken??
     * RequestToken??????????{@code null}?
     *
     * @param requestToken RequestToken
     * @param pinCode ???PIN
     * @return AccessToken??????????{@code null}
     */
    public AccessToken requestAccessToken(RequestToken requestToken, String pinCode) {
        Twitter twitter = TwitterFactory.getSingleton();

        AccessToken accessToken = null;
        try {
            accessToken = twitter.getOAuthAccessToken(requestToken, pinCode);
            twitter.setOAuthAccessToken(accessToken);
        } catch (IllegalStateException e) {
            System.err.println(ErrorCode.NO_TOKEN_AVAILABLE);
        } catch (TwitterException e) {
            System.err.println(ErrorCode.TWITTER_AUTHORIZATION_FAILED);
        }

        return accessToken;
    }
}