Working with Twitter API 1.1 with Unity

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

BitBucket Git Repo

About these ads
Tagged , , , ,

11 thoughts on “Working with Twitter API 1.1 with Unity

  1. David says:

    I was trying to use the Let’s Tweet in Unity plugin to make just a simple tweet, since it has been made for 1.0 I tried to put him working for 1.1, but it’s giving me an “STATUS: HTTP/1.1 401 Unauthorized” can you help me please?

    • yaustar says:

      What changes have you made to the ‘Let’s Tweet in Unity’ plugin to try and get it to work with API 1.1?

      • David says:

        I just changed the URL since it was outdated and the request when printed in console seemed ok. I found this rep:

        It works but they are using ” Application.OpenURL()” to open the page to ask permissions to user and get PIN to generate the token, and for mobile that’s not very user friendly is there another way of getting the PIN?

      • yaustar says:

        Are you looking to post tweets from your own account or have someone be able to login via your application and tweet from their own account?

      • David says:

        Have someone login via my application and tweet from their own account.

      • yaustar says:

        In that case, you will end up with a similar use flow issue as the app will have to request permission from the Twitter via the user. i.e The user will enter login details, the app will open a page or open the Twitter app asking the user for permission (usually having a button that says ‘Allow’) to allow the app to post on their behalf.


        This isn’t something I have done from scratch yet and if you are using mobile then I can recommend Prime31’s Social Networking Plugin which is $65 but does the job pretty well in my experience.

        If I do get some time, I may look into it as I am a little interested in the API flow from Twitter.

      • rebolomo says:

        you have to change the code in this way:
        #region Twitter API Methods

        //private static readonly string PostTweetURL = “{0}”;

        private static readonly string PostTweetURL = “”;
        public static IEnumerator PostTweet(string text, string consumerKey, string consumerSecret, AccessTokenResponse response, PostTweetCallback callback)
        if (string.IsNullOrEmpty(text) || text.Length > 140)
        Debug.Log(string.Format(“PostTweet – text[{0}] is empty or too long.”, text));

        string url = PostTweetURL;//string.Format(PostTweetURL, UrlEncode(text));
        Dictionary parameters = new Dictionary();

        parameters.Add(“status”, text);

        // Need to fill body since Unity doesn’t like an empty request body.
        byte[] dummmy = new byte[1];
        dummmy[0] = 0;

        // HTTP header
        Hashtable headers = new Hashtable();
        headers["Authorization"] = GetHeaderWithAccessToken(“POST”, url, consumerKey, consumerSecret, response, parameters);

        WWWForm form = new WWWForm ();

        form.AddField (“status”, text);

        byte[] rawData =;

        WWW web = new WWW(url, rawData, headers);
        yield return web;

        if (!string.IsNullOrEmpty(web.error))
        Debug.Log(string.Format(“PostTweet – failed. {0}”, web.error));
        string error = Regex.Match(web.text, @”([^&]+)”).Groups[1].Value;

        if (!string.IsNullOrEmpty(error))
        Debug.Log(string.Format(“PostTweet – failed. {0}”, error));


  2. cheliotk says:

    Nice, this implementation works great!
    How do you access the next page results though, when results are more than the count? Do you need to re-authorize the request or would a simple WWW query with the next page results URL (without headers etc) work?

    • yaustar says:

      For the next ‘page’, you have to use the ‘since_id’ and ‘max_id’ parameters to navigate between ‘pages’ of tweets. The links on this page ( will explain more.

      I may update the sample to handle this as it wasn’t something I looked at before.

      Not sure about needing to re-authorization but I am guessing yes due to the timestamp used.

  3. Very nice, thanks! I ran into one bug:
    SearchTwitter_Coroutine hardcodes a search for #gdc instead of using the keywords.
    I changed it to this: {“q”, keywords}, and it seems to work fine…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 200 other followers

%d bloggers like this: