Twitter REST API is very finicky

The post title says it all.

I have been trying to put together some code that calls the Twitter REST API, and found it to be extremely finicky. If you do not have everything just right, you will get back a frustrating error message that says “Could not authenticate you” and “code 32”. Not super helpful.

Most of the issues I had were in building the base string that gets sent to Twitter. If this string is not built exactly as it needs to be built, things will just not work.

Here is what I found about building the base string through trial and error, and with exhaustive searches of the Google and Stack Overflow:

  • After the endpoint URL, make sure that your query string parameters are in alphabetical order
  • If you are putting into the base string part of a query that needs to be URL encoded, then it needs to be double encoded inside the base string

For example, on this double encoding thing, if you want to search for “Twitter API”, the search part of your query string, once URL encoded would look like this:

%26q%3DTwitter%20API

However, you need to double encode the search text so that it looks like this instead:

%26q%3DTwitter%2520API

BTW, Happy Birthday to Brian May, guitarist from the legendary rock group Queen.

Leave a Reply