How to do OAuth authentication via AJAX / jQuery?

13,093

Okay, if I understand what you are trying to do is to make an Ajax call from a web page to the Twitter API and post/retrieve tweets and other info from Twitter.

Since the release of the API v1.1, Twitter has deprecated the v1.0 API and one of the major changes in 1.1 was Authentication Required on all Endpoints

And to do this from JavaScript and jQuery is quite possible (albeit very cumbersome, difficult and requires the use of many 3rd party JS libraries to HMAC Hash your data and keys and calculate content lengths on the client side before making your Request. Twitter API does not support CORS but does support JSONP for these kind of Ajax requests. But this is not recommended - since doing this on the client side will require you to have your Twitter App Access Keys - Private keys - embedded in your script files - which is basically a big NO-NO. And hence a server side solutions to generate your oAuth tokens is recommended. But once you have achieved that, it may be easier to get the token on your script and make Ajax calls using that from the browser. But I haven't gone that far in my research.

Also, this is based off of my research in Mid 2013 when my Twitter Ajax widgets stopped working because of this change and I gave up trying to fix it using that route after I realized it would compromise my security keys. Things may have changed since then.

If you are still interested to find a solution, this walkthrough would be a good place to start learning about Twitter's oAuth and how the Access Tokens are generated: https://dev.twitter.com/docs/auth/oauth

Share:
13,093
diegoaguilar
Author by

diegoaguilar

I'm a mexican backend web developer interested in performance and infrastructure development. I've been working with Node/express mainly for last 3 years doing API's design and implementation. Been pretty involved with MEAN apps too, and pretty involved with MongoDB in particular. I really can say I love express before frameworks. About languages, Right now I'm pretty involved with Javascript, with new ES6 and ES7 stuff. I've got experience with Java and Python too, can even work a bit in Android what I've liked. Would love to get to know Go, Elixir and these trending functional programming languages soon ;) Image processing and natural language processing are challenging topics for me, and I'd love to lead my career to high performance and availability aplications and stacks development. I rather do backend development but lately I've been trying React, which I enjoy. Apart from software development, I love Real Madrid and watching movies and series, of course music, and I also love food, like a lot.

Updated on June 04, 2022

Comments

  • diegoaguilar
    diegoaguilar almost 2 years

    I've worked a bit with Twitter web API, I know it works with OAuth, I've consumed the API using a python library. I've also tried a bit of Instagram API using a Java Script small library.

    I know those perform web REST requests in the background, authenticating first and then querying requests as I code.

    However, what if I want to perform the requets using jQuery $.ajax from a web application?

    I've read some docs and sites and it seems it's just possible. Like only ajaxing to the API routes, starting with the authentication route.

    But, how does this process work? I mean, I query by AJAX to the auth route and then how do I keep track of that authentication. How to keep that communication? Will the redirect URL play its role then?

    Reading this site for Instagram API I start getting a clue about it, but got the doubts mentioned above.

    I want to perform all AJAX requests in the Java Script server background (I'm using node.js), assuming I will provide my apps OAuth in the $.ajax. Is that OK or I can actually code it on client site keeping my OAuth tokens save?

    And, if it's concern of this same question, when it comes to bytes (pictures, sound, etc) how to catch the response from API.