Skip to content

misenhower/MicroTweet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MicroTweet

MicroTweet is a Twitter API Library for the .NET Micro Framework. It can be used to post and retreive tweets directly from a NETMF device via Twitter's API.

MicroTweet requires NETMF v4.3 and a board that supports SSL.

Getting Started

A sample application is included that shows a few of the primary functions of MicroTweet (including sending a tweet and retrieving your home timeline). You can base your application off the included sample, or start a new application that references the MicroTweet library.

Using MicroTweet in your own application

To reference the MicroTweet library, you can either add the MicroTweet project to your VS solution or simply install the library via NuGet.

To install it from NuGet, run the following command in the Package Manager Console:

Install-Package MicroTweet

Instructions for using NuGet and the Package Manager Console can be found here.

Once you've referenced the MicroTweet assembly from your application, add a using directive for the MicroTweet namespace at the top of your program file (e.g., Program.cs):

using MicroTweet;

Also, if you use DHCP, you may want to wait for an IP address to be acquired before performing any network operations. You can do this by checking the value of IPAddress.GetDefaultLocalAddress() at the beginning of your program:

while (IPAddress.GetDefaultLocalAddress() == IPAddress.Any)
    Thread.Sleep(50);

Setting the current time

Important: Twitter API requests must contain a valid (current) timestamp. For your convenience, an SNTP class is included with MicroTweet to retrieve the current time from an NTP server.

You can update the current time on your board with the following code:

Microsoft.SPOT.Hardware.Utility.SetLocalTime(Sntp.GetCurrentUtcTime());

Initializing TwitterClient

To use the Twitter API from your application you must first register it with Twitter to get a set of API keys. Go to the Twitter Application Manager page to create a new application and get the necessary keys.

You will get two sets of keys: one for the application you created and one for your user account to access the API through that application. These are known as the application credentials and the user credentials.

You can initialize a new instance of TwitterClient with these keys as follows:

// Set up application and user credentials
var appCredentials = new OAuthApplicationCredentials()
{
    ConsumerKey = "YOUR_CONSUMER_KEY_HERE",
    ConsumerSecret = "YOUR_CONSUMER_SECRET_HERE",
};
var userCredentials = new OAuthUserCredentials()
{
    AccessToken = "YOUR_ACCESS_TOKEN",
    AccessTokenSecret = "YOUR_ACCESS_TOKEN_SECRET",
};

// Create new Twitter client with these credentials
var twitter = new TwitterClient(appCredentials, userCredentials);

Sending a Tweet

Use the SendTweet method to send a tweet:

try
{
    var tweet = twitter.SendTweet("Trying out MicroTweet!");
}
catch (Exception e)
{
    // Couldn't send the tweet, the exception may have more information
}

If an error is received from Twitter's API, a TwitterException will be thrown with further details. The Message property of the exception will contain the actual error message received from Twitter.

Retrieving your home timeline

Use the GetHomeTimeline method to retrieve the most recent tweets and retweets posted by people you follow:

try
{
    var tweets = twitter.GetHomeTimeline();
    foreach (Tweet tweet in tweets)
        Debug.Print("Tweet from @" + tweet.User.ScreenName + ": \"" + tweet.Text + "\"");
}
catch (Exception e)
{
    // Couldn't retrieve the timeline, the exception may have more information
}

GetHomeTimeline accepts two arguments, sinceID and maxID, to specify the range of tweets to retrieve. You may want to use named arguments to specify these values. For example:

var tweets = twitter.GetHomeTimeline(maxID: 603281686600683520);

Retrieving a specific user's timeline

Use the GetUserTimeline method to view the tweets and retweets posted by a specific user:

try
{
    var tweets = twitter.GetUserTimeline("twitter");
    foreach (Tweet tweet in tweets)
        Debug.Print("Tweet from @" + tweet.User.ScreenName + ": \"" + tweet.Text + "\"");
}
catch (Exception e)
{
    // Couldn't retrieve the timeline, the exception may have more information
}

Like GetHomeTimeline, you can specify the range of tweets to retrieve with sinceID and maxID:

var tweets = twitter.GetUserTimeline("twitter", sinceID: 600728701584576512);

Other features

The TwitterClient class has a few other useful methods:

  • GetCurrentUser can be used to verify the supplied credentials (API keys). If the credentials are valid, a User object will be returned with details for the authenticating user.
  • GetUser retrieves information for the user with the specified screen name or ID.

About

Twitter API Library for the .NET Micro Framework

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published