From 11a75a7179bc05fb763959bb7ef25579a6e630f8 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Thu, 13 May 2021 18:38:09 -0700 Subject: [PATCH] Moved TwitterOAuth to Kotlin. --- .../thauvin/erik/mobibot/TwitterOAuth.java | 111 ----------------- .../net/thauvin/erik/mobibot/TwitterOAuth.kt | 113 ++++++++++++++++++ 2 files changed, 113 insertions(+), 111 deletions(-) delete mode 100644 src/main/java/net/thauvin/erik/mobibot/TwitterOAuth.java create mode 100644 src/main/kotlin/net/thauvin/erik/mobibot/TwitterOAuth.kt diff --git a/src/main/java/net/thauvin/erik/mobibot/TwitterOAuth.java b/src/main/java/net/thauvin/erik/mobibot/TwitterOAuth.java deleted file mode 100644 index 2690710..0000000 --- a/src/main/java/net/thauvin/erik/mobibot/TwitterOAuth.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * TwitterOAuth.java - * - * Copyright (c) 2004-2021, Erik C. Thauvin (erik@thauvin.net) - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * Neither the name of this project nor the names of its contributors may be - * used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package net.thauvin.erik.mobibot; - -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; - -/** - * The TwitterOAuth class. - *

- * Go to http://twitter.com/oauth_clients/new to register your bot. - *

- * Then execute: - *

- * - * java -cp "mobibot.jar:lib/*" net.thauvin.erik.mobibot.TwitterOAuth <consumerKey> <consumerSecret> - * - *

- * and follow the prompts/instructions. - * - * @author Erik C. Thauvin - * @author Twitter4J - * @created Sep 13, 2010 - * @since 1.0 - */ -@SuppressWarnings("PMD.UseUtilityClass") -public final class TwitterOAuth { - /** - * Twitter OAuth Client Registration. - * - * @param args The consumerKey and consumerSecret should be passed as arguments. - * @throws TwitterException If an error occurs. - * @throws IOException If an IO error occurs. - */ - @SuppressWarnings({ "PMD.SystemPrintln" }) - public static void main(final String[] args) throws TwitterException, IOException { - if (args.length == 2) { - final twitter4j.Twitter twitter = new TwitterFactory().getInstance(); - twitter.setOAuthConsumer(args[0], args[1]); - final RequestToken requestToken = twitter.getOAuthRequestToken(); - AccessToken accessToken = null; - try (final 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) or just hit enter.[PIN]:"); - final String pin = br.readLine(); - try { - if (pin != null && pin.length() > 0) { - accessToken = twitter.getOAuthAccessToken(requestToken, pin); - } else { - accessToken = twitter.getOAuthAccessToken(); - } - - System.out.println( - "Please add the following to the bot's property file:" + "\n\n" + "twitter-consumerKey=" - + args[0] + '\n' + "twitter-consumerSecret=" + args[1] + '\n' + "twitter-token=" - + accessToken.getToken() + '\n' + "twitter-tokenSecret=" + accessToken - .getTokenSecret()); - } catch (TwitterException te) { - if (401 == te.getStatusCode()) { - System.out.println("Unable to get the access token."); - } else { - te.printStackTrace(); - } - } - } - } - } else { - System.out.println("Usage: " + TwitterOAuth.class.getName() + " "); - } - - System.exit(0); - } -} diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/TwitterOAuth.kt b/src/main/kotlin/net/thauvin/erik/mobibot/TwitterOAuth.kt new file mode 100644 index 0000000..31bde39 --- /dev/null +++ b/src/main/kotlin/net/thauvin/erik/mobibot/TwitterOAuth.kt @@ -0,0 +1,113 @@ +/* + * TwitterOAuth.kt + * + * Copyright (c) 2004-2021, Erik C. Thauvin (erik@thauvin.net) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of this project nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.thauvin.erik.mobibot + +import twitter4j.TwitterException +import twitter4j.TwitterFactory +import twitter4j.auth.AccessToken +import java.io.BufferedReader +import java.io.IOException +import java.io.InputStreamReader +import kotlin.system.exitProcess + +/** + * The `TwitterOAuth` class. + * + * Go to [https://developer.twitter.com/en/apps](https://developer.twitter.com/en/apps) to register your bot. + * + * Then execute: + * + * `java -cp mobibot.jar net.thauvin.erik.mobibot.TwitterOAuth ` + * + * and follow the prompts/instructions. + * + * @author [Erik C. Thauvin](https://erik.thauvin.net) + * @author [Twitter4J Example](https://twitter4j.org/en/code-examples.html#oauth) + */ +object TwitterOAuth { + /** + * Twitter OAuth Client Registration. + * + * @param args The consumerKey and consumerSecret should be passed as arguments. + */ + @Throws(TwitterException::class, IOException::class) + @JvmStatic + fun main(args: Array) { + if (args.size == 2) { + with(TwitterFactory.getSingleton()) { + setOAuthConsumer(args[0], args[1]) + val requestToken = oAuthRequestToken + var accessToken: AccessToken? = null + BufferedReader(InputStreamReader(System.`in`)).use { br -> + while (null == accessToken) { + print( + """ + Open the following URL and grant access to your account: + + ${requestToken.authorizationURL} + + Enter the PIN (if available) or just hit enter. [PIN]: """.trimIndent() + ) + val pin = br.readLine() + try { + accessToken = if (!pin.isNullOrEmpty()) { + getOAuthAccessToken(requestToken, pin) + } else { + oAuthAccessToken + } + println( + """ + Please add the following to the bot's property file: + + twitter-consumerKey=${args[0]} + twitter-consumerSecret=${args[1]} + twitter-token=${accessToken?.token} + twitter-tokenSecret=${accessToken?.tokenSecret} + """.trimIndent() + ) + } catch (te: TwitterException) { + @Suppress("MagicNumber") + if (401 == te.statusCode) { + println("Unable to get the access token.") + } else { + te.printStackTrace() + } + } + } + } + } + } else { + println("Usage: ${TwitterOAuth::class.java.name} ") + } + exitProcess(0) + } +}