jonnyreeves / fetch-readablestream

Compatibility layer for efficient streaming of binary data using WHATWG Streams
MIT License
49 stars 10 forks source link

Query on the working #5

Closed GoChartingAdmin closed 6 years ago

GoChartingAdmin commented 6 years ago

How would I use this to stream using the Twitter Stream API. Can I pass the credentials like below

var options = {
    host : 'stream.twitter.com',
    path : '/1.1/statuses/filter.json?track=query',
    method : 'GET',
    headers : {"Authorization": "Basic "+new Buffer("username:password").toString("base64")}
};

function readAllChunks(readableStream) {
  const reader = readableStream.getReader();
  const chunks = [];

  function pump() {
    return reader.read().then(({ value, done }) => {
      if (done) {
        return chunks;
      }
      chunks.push(value);
      return pump();
    });
  }

  return pump();
}

fetchStream(options)
  .then(response => readAllChunks(response.body))
  .then(chunks => console.dir(chunks))
jonnyreeves commented 6 years ago

If you take a look at https://github.com/jonnyreeves/fetch-readablestream/blob/master/src/fetch.js you will see that fetchStream / fetchRequest expect to be invoked with the following call signature:


const url = 'https://stream.twitter.com/1.1/statuses/filter.json?track=query';

const options = {

    method : "GET",
    headers : { "Authorization": `Basic ${new
Buffer("username:password").toString("base64")}` }

};

fetchStream(url, options)

  .then(response => readAllChunks(response.body))
  .then(chunks => console.dir(chunks));

On Fri, 30 Mar 2018 at 15:35 GoChartingAdmin notifications@github.com wrote:

How would I use this to stream using the Twitter Stream API. Can I pass the credentials like below

var options = { host : 'stream.twitter.com', path : '/1.1/statuses/filter.json?track=query', method : 'GET', headers : {"Authorization": "Basic "+new Buffer("username:password").toString("base64")} };

function readAllChunks(readableStream) { const reader = readableStream.getReader(); const chunks = [];

function pump() { return reader.read().then(({ value, done }) => { if (done) { return chunks; } chunks.push(value); return pump(); }); }

return pump(); }

fetchStream(options) .then(response => readAllChunks(response.body)) .then(chunks => console.dir(chunks))

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jonnyreeves/fetch-readablestream/issues/5, or mute the thread https://github.com/notifications/unsubscribe-auth/AAMN-Xd24Y6jvXq8Q9Z6W89G_TS4E4Hrks5tjkK6gaJpZM4TBwfx .

GoChartingAdmin commented 6 years ago

Thanks. A follow-up query which is not directly related to the repo and hence will close it for now. I am getting a authorization fail. Do we need to pass TWITTER KEY, TWITTER SECRET etc. If yes, how do I layer it into the code. It is perfectly fine if you suggest I take this to stackoverflow

        const url = "https://stream.twitter.com/1.1/statuses/filter.json?track=query";
        const options = {
            method: "POST",
            mode: "no-cors",
            headers: {Authorization: `Basic ${new Buffer("username:password").toString("base64")}`}
        };
        fetchStream(url, options)
            .then(response => readAllChunks(response.body))
            .then(chunks => console.log(chunks));

Above did not work nor did the below attempt to authenticate and get a bearer token

const url = "https://api.twitter.com/oauth2/token?grant_type=client_credentials";
        const consumerKey = "xxxx";
        const consumerSecret = "xxxxx"
        const base64EncodedKey = new Buffer(`${consumerKey}:${consumerSecret}`).toString("base64")
        const options = {
            method: "POST",
            mode: "no-cors",
            headers: {
                "Authorization": `Basic ${base64EncodedKey}`,
                "Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"
            }
        };
        fetchStream(url, options)
            .then(response => console.log(response))

https://api.twitter.com/oauth2/token?grant_type=client_credentials 403 ()