Using the Twitter REST API v1.1 from Azure Mobile Services

Thursday, June 20, 2013

Since the retirement of the Twitter REST API v1, I have been having trouble doing a simple query using the new search API introduced in version 1.1 from Azure Mobile Services because it now requires a signed authentication header in the request using the OAuth 1.0 protocol.

I would suggest reading the tutorial on Schedule recurring jobs in Mobile Services which provides a basic walk through on creating a scheduled job in Azure Mobile Services that requests tweets from Twitter and stores it in a table. The reason you should read this tutorial is to gain some background knowledge on the concept for better understanding of my newly devised method of use.

The problem with the [out-dated] tutorial mentioned above is that it still uses version 1 of the recently depreciated Twitter REST API.

Current (old) approach

Here's the code for the scheduler borrowed from the [current] tutorial:

After plenty of research (and failed attempts) on properly signing the request to have Twitter authenticate it using custom code, I realised that Azure Mobile Services is hosted by a NodeJs process and learned that the 'Request'-object comes out-of-the-box with OAuth support.

New (updated) approach

By simply assigning the application's keys and tokens to the OAuth property of the request, it worked like a charm:


Do take note

There are some breaking changes to the API therefor I strongly recommend studying the Twitter API 1.1 documentation. Some of the other changes I had to make to the original code include changing the expected response body property 'results' to 'statuses' as well as the mapping to the.'update' object.

I hope that this saves someone out there somewhere a lot of trouble and time.

Your comments and tweets are welcome. @FanieReynders.

Till next time!

6 comments

  1. this is great, it is exactly what i am looking for great job. I am
    having one problem. everything is working great, but if i change the search to something like ...search.json?q=from:realtimewwII it is doing the can't connect to twitter error. change it back to your search and it works great any idea?
    thanks
    tim

    ReplyDelete
    Replies
    1. Thanks Tim! Do take note that the endpoint for the search API changed in version 1.1. You are referencing the retired version of the API.

      Change the URL from 'http://search.twitter.com/search.json?q=...' to 'https://api.twitter.com/1.1/search/tweets.json?q=...'

      Delete
    2. Hi Fanie,
      my fault i copied the code wrong, this is the actual code line. var url = "https://api.twitter.com/1.1/search/tweets.json?q=from:realtimewwII"; And it is still giving me an error that it can not connect to twitter. does not make sense to me because i can use the from:realtimewwII in twitter's search page and it works fine. any ideas? my code is your code i copied it and pasted it. i have the updates table created to match the columns that are returned. i was going to modify things once i can get the search query to work.

      tim

      Delete
    3. Hi Tim,
      See if its an encoding problem maybe? By encoding the colon (:) with html encoding might just work.

      otherwise please share your snippet as a Gist on Github and I will gladly take a look for you.

      Delete
    4. Hi Fanie,

      You were spot on, it did not like the ":" between the from and the author i was looking for. i changed the : to %3A and everything started to work great. thanks for pointing me in the correct direction

      thanks again this is some great info you supplied.

      tim

      Delete
  2. Bạn đang lo lắng phân vân khi không biết nên đặt niềm tiên ở công ty thiết kế web nào.? Bạn chưa bít giá cả và chất lượng dịch vụ ra sao.? Tôi xin giới thiệu:
    Công Ty TNHH HTSolution là một công ty thiết kế website chuyên cung cấp các dịch vụ chuyên nhiệp như: Thiết kế website giá rẻ, phần mềm quản lý quán cà phê, cần nâng cấp website….vv.
    Nếu bạn có nhu cầu sử dụng dịch vụ phần mềm quản lý hay cần làm web của công ty chúng tôi vui lòng liên hệ:
    CÔNG TY TNHH HTSOLUTION
    Địa chỉ: 78th Thăng Long - P.4 - Q.Tân Bình - TP.HCM
    Điện thoại: (08) 6267 3377 (Anh Hoàng) or 0985327297 (Anh Trường)
    Email: Info@htsolution.vn

    ReplyDelete

Community

Popular Posts

Archives

Contributors