Skip to content

Instantly share code, notes, and snippets.

@kiy0taka
Created June 11, 2011 05:29
Show Gist options
  • Select an option

  • Save kiy0taka/1020286 to your computer and use it in GitHub Desktop.

Select an option

Save kiy0taka/1020286 to your computer and use it in GitHub Desktop.
@Grab('org.twitter4j:twitter4j-core:[2.1,)')
import twitter4j.*
new TwitterFactory().instance.updateStatus "${args[0]} #twitter4j_reading"
@Grab('org.twitter4j:twitter4j-core:2.2.3')
import twitter4j.*
import twitter4j.auth.*
import java.awt.Desktop
def CONSUMER_KEY = "xxxxxxxxxxxxxxxxxxxx"
def CONSUMER_SECRET = "xxxxxxxxxxxxxxxxxxxx"
def twitter = new TwitterFactory().instance
twitter.setOAuthConsumer CONSUMER_KEY, CONSUMER_SECRET
RequestToken requestToken = twitter.OAuthRequestToken
AccessToken accessToken
while (null == accessToken) {
Desktop.desktop.browse new URI(requestToken.authorizationURL)
def pin = System.in.withReader {
print "Enter the PIN(if aviailable) or just hit enter.[PIN]:"
it.readLine()
}
try {
if (pin) {
accessToken = twitter.getOAuthAccessToken(requestToken, pin)
} else {
accessToken = twitter.OAuthAccessToken
}
} catch (TwitterException te) {
if (401 == te.getStatusCode()) {
println "Unable to get the access token."
} else {
te.printStackTrace()
}
System.exit(1)
}
}
new File('twitter4j.properties').text = """\
oauth.consumerKey=$CONSUMER_KEY
oauth.consumerSecret=$CONSUMER_SECRET
oauth.accessToken=${accessToken.token}
oauth.accessTokenSecret=${accessToken.tokenSecret}"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment