bytespider / jsOAuth

JavaScript implimentation of the OAuth protocol. Currently supports version 1.0 (RFC5849) of the specification. Node.js & CommonJS compatible.
http://bytespider.github.com/jsOAuth/
MIT License
557 stars 109 forks source link

Support streaming responses #29

Open bytespider opened 12 years ago

bytespider commented 12 years ago

A feature request: I'm playing with Twitter's Streaming API, which requires OAuth for connecting. Problem is jsOAuth does not support streaming responses.

The get method currently looks at readystate == 4, which is the completed state. A streaming response never does complete. So I need to write my own request method, that does that magic I want. Problem is, the request method is HUGE, and while I only need to monkey patch a small part of the code, the size of the request method makes me have to copy/paste large chunks of code. So it all sums up to this: I would like request method to be split up into several smaller methods, so I can write a method that works with streaming.

EmilStenstrom commented 12 years ago

To clarify my use-case even more:

I would like to listen to readystate == 3, and read the partial response from xhr.responseText. This does not work in IE, so I don't expect support for this in jsOAuth. What I would like, is a way to write the plumbing for that myself, and somehow just use the OAuth signing from jsOAuth.

bytespider commented 12 years ago

How about something like:

    var xhr = oauth.signedXMLHttpRequest({
        method: "GET",
        url: "http://bytespider.eu",
        data: {message: "this is cool"},
        headers: {"Content-Type": "application/x-www-form-urlencoded"}
    });

Clearly this method you'd have to handle some stuff yourself like how to encode any data you need to send.

Alternatively: I can add readystatechange as an option to oauth.request() which would replace the built in one.

EmilStenstrom commented 12 years ago

Yes, the first alternative sounds like a great one!

EmilStenstrom commented 12 years ago

Here's a working example of what I'm trying to do: http://stackoverflow.com/a/8752044/117268 - I just need OAuth instead of Basic Auth now.

sfrenot commented 12 years ago

Hello guy, I have made the oauth work with streaming twitter. It is mostly working from the origin but there were some problems.

I had to remove the header callback, since keys were not well calculated.

headerParams = { //'oauth_callback': oauth.callbackUrl, ...

As, you said I had to manage the stream away from this condition : if (xhr.readyState === 4) { I put some very bad stuff before, just to see that it works, such as xhr.onreadystatechange = function () { success(xhr); if (xhr.readyState === 4) {

bytespider commented 12 years ago

I'm sorry I don't understand. Can you be clearer? Please submit a pull request if you have code you would like to share.

bguiz commented 12 years ago

+1 vote for this as well - I asked this question prior to finding this issue: http://stackoverflow.com/questions/11374771/how-to-make-twitter-streaming-apis-work-with-jsoauth

bytespider commented 12 years ago

jsOAuth 2.0 will support a way to craft and use your own XHR objects and callbacks.

bguiz commented 12 years ago

@bytespider Thanks for the update - looking forward to 2.0! Any word on the rough timeline to expect for this?

bytespider commented 12 years ago

Sadly not. I may do a pre-release without OAuth 2.0 support just to get the code out there.

bguiz commented 12 years ago

@bytespider Yeah that pre-release sounds like a great idea!