Skip to content

Instantly share code, notes, and snippets.

@posaunehm
Created March 31, 2013 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save posaunehm/5280852 to your computer and use it in GitHub Desktop.
Save posaunehm/5280852 to your computer and use it in GitHub Desktop.
Get timeline using twitter4j in C# (via IKVM)
using java.awt;
using java.io;
using java.lang;
using java.net;
using java.util;
using twitter4j;
using twitter4j.auth;
using System.Collections.Generic;
namespace t4ikvm
{
internal class Program
{
private static void Main(string[] args)
{
var file = new File("twitter4j.properties");
var prop = new Properties();
InputStream inputStream = null;
OutputStream outputStream = null;
try
{
if (file.exists())
{
inputStream = new FileInputStream(file);
prop.load(inputStream);
}
if (args.Length < 2)
{
if (null == prop.getProperty("oauth.consumerKey")
&& null == prop.getProperty("oauth.consumerSecret"))
{
// consumer key/secret are not set in twitter4j.properties
System.Console.WriteLine(
"Usage: java twitter4j.examples.oauth.GetAccessToken [consumer key] [consumer secret]");
System.Environment.Exit(-1);
}
}
else
{
prop.setProperty("oauth.consumerKey", args[0]);
prop.setProperty("oauth.consumerSecret", args[1]);
outputStream = new FileOutputStream("twitter4j.properties");
prop.store(outputStream, "twitter4j.properties");
}
}
catch (IOException ioe)
{
ioe.printStackTrace();
System.Environment.Exit(-1);
}
finally
{
if (inputStream != null)
{
try
{
inputStream.close();
}
catch (IOException ignore)
{
}
}
if (outputStream != null)
{
try
{
outputStream.close();
}
catch (IOException ignore)
{
}
}
}
try
{
Twitter twitter = new TwitterFactory().getInstance();
RequestToken requestToken = twitter.getOAuthRequestToken();
System.Console.WriteLine("Got request token.");
System.Console.WriteLine("Request token: " + requestToken.getToken());
System.Console.WriteLine("Request token secret: " + requestToken.getTokenSecret());
AccessToken accessToken = null;
BufferedReader br = new BufferedReader(new InputStreamReader(java.lang.System.@in));
while (null == accessToken)
{
System.Console.WriteLine("Open the following URL and grant access to your account:");
System.Console.WriteLine(requestToken.getAuthorizationURL());
try
{
Desktop.getDesktop().browse(new URI(requestToken.getAuthorizationURL()));
}
catch (UnsupportedOperationException ignore)
{
}
catch (IOException ignore)
{
}
catch (URISyntaxException e)
{
throw new AssertionError(e);
}
System.Console.WriteLine("Enter the PIN(if available) and hit enter after you granted access.[PIN]:");
var 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.Console.WriteLine("Unable to get the access token.");
}
else
{
te.printStackTrace();
}
}
}
System.Console.WriteLine("Got access token.");
System.Console.WriteLine("Access token: " + accessToken.getToken());
System.Console.WriteLine("Access token secret: " + accessToken.getTokenSecret());
try
{
prop.setProperty("oauth.accessToken", accessToken.getToken());
prop.setProperty("oauth.accessTokenSecret", accessToken.getTokenSecret());
outputStream = new FileOutputStream(file);
prop.store(outputStream, "twitter4j.properties");
outputStream.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
System.Environment.Exit(-1);
}
finally
{
if (outputStream != null)
{
try
{
outputStream.close();
}
catch (IOException ignore)
{
}
}
}
System.Console.WriteLine("Successfully stored access token to " + file.getAbsolutePath() + ".");
try
{
var user = twitter.verifyCredentials();
var statuses = twitter.getHomeTimeline().ToEnumerable<Status>();
System.Console.WriteLine("Showing @" + user.getScreenName() + "'s home timeline.");
foreach (var status in statuses)
{
System.Console.WriteLine("@" + status.getUser().getScreenName() + " - " + status.getText());
}
}
catch (TwitterException te)
{
te.printStackTrace();
System.Console.WriteLine("Failed to get timeline: " + te.getMessage());
System.Environment.Exit(-1);
}
System.Environment.Exit(0);
}
catch (TwitterException te)
{
te.printStackTrace();
System.Console.WriteLine("Failed to get accessToken: " + te.getMessage());
System.Environment.Exit(-1);
}
catch (IOException ioe)
{
ioe.printStackTrace();
System.Console.WriteLine("Failed to read the system input.");
System.Environment.Exit(-1);
}
}
}
public static class JavaListExtention
{
public static IEnumerable<T> ToEnumerable<T>(this java.util.List jList)
{
var iterator = jList.iterator();
while (iterator.hasNext())
{
yield return (T)iterator.next();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment