Update 9th Oct 2014: Let’s Tweet in Unity has been updated to work with API 1.1: https://www.assetstore.unity3d.com/en/#!/content/536
Back around late summer/early autumn, I was working on a project that involved showing Tweets in a unique way using Unity. The concept itself was not complicated and with Unity being Unity, we thought there would be a plugin or existing code to work with Twitter’s API and we could prototype the concept relatively quickly.
We were wrong. It took us a few days between us to get the search query working with Unity using Twitter API 1.1.
There are quite a few resources and plugins for Twitter’s old API (1.0) but none for 1.1. 1.0 was a really simple interface in terms of producing a URL query and the result would be a JSON string. 1.1 added the need to use OAuth authentication which has caused problems for many developers judging from the various forums littered with queries.
At this stage we had two problems, getting the OAuth Authentication correct and working with Unity’s WWW class for web queries instead of the .NET framework.
Twitter’s OAuth authentication is incredibly strict and even after trying several OAuth libraries (including the one I used with my previous Yelp OAuth entry) I had little luck. Thankfully, Twitter provides an OAuth Tool to that that creates the Signature Base String and Authorisation Header based on the queries allowing you to check if your implementation is correct. This was a huge help for us as it meant we could isolate the OAuth section of code and work on getting the signature correct before making a query to the Twitter servers. Just be sure to override the OAuth Nounce (which is normally a random number) and OAuth Timestamp to match the values used in Twitter’s tool otherwise you will get different results.
For us, it turned out to be not including correct parameters and also not having them in alphabetical order. We ended using a code sample from Steven Mosley’s blog and modifying it for our purposes.
Our next problem was a little more tricky and we wasted time because we read outdated information about was possible using the WWW class and could cause errors. The basic search API was a GET query with parameters which meant we had to pass an object to the postData parameter. However, the Unity documentation states that passing an object in the postData parameter in the WWW constructor call would produce a POST query which would cause the API to fail on the Twitter servers and reading the comments from Let’s Tweet in Unity plugin code, passing a null would cause an exception to fire.
We wasted a solid day looking for a way to make a GET web query in Unity without the WWW class which proved to be fruitless and was ready to setup a local web server to make the Twitter that our prototype in Unity could access as a proxy. It was until I spoke to a few other developers on The Chaos Engine that it was possible to pass null in the WWW postData parameter and it would remain as a GET query without crashing.
After that, everything was working as it should and we had finally make search queries to Twitter.
Below is a link to a basic sandbox Unity product with a slightly more user friendly version of the code. It hasn’t been fully tested and just comes ‘as is’ but should be enough to get you going pretty quickly should you need it. The source can be found in Assets/Scripts