Skip to content

unable to stream API response when token is refreshed #260

@yoavniran

Description

@yoavniran

Hello, please keep in mind im fairly new to node development so some of my assumptions may be wrong.

Im working on an app that allows downloading files from g-drive offline, as in the server keeps the user's token so it can access their files without needing to login every time.

I want to be able to stream the content of a drive file to the fs so I use a write stream (fs.createWriteStream).
I tried using the oauth2client (https://github.com/google/google-api-nodejs-client/blob/master/lib/auth/oauth2client.js) but that proved impossible for the reason that in all likelihood, the token stored by the app has expired and requires refreshing.

In short, pipe works when the token doesnt require refreshing and doesnt when the token has expired.

In detail, the issue is that if you follow the flow of the oauth2client code (specifically the request method) you see that it checks the expiry date and if passed it will make a request to refresh the token. This request is over HTTP so is asynchronous. At this point, the request method will not be able to return the result from calling "this._makeRequest..." which is actually the return object from calling on the request module (https://github.com/mikeal/request), which is the object needed for calling "pipe()" on.

Unfortunately, this meant I had to write my own client/transporter which is what I did here:
https://gist.github.com/yoavniran/06454e87a3b8d858614e#file-simpleauthtransporter-nodejs
My class relies on emitting events so it can expose the request object even if a token refresh is needed.

I didn't want to write my own code for this but couldnt find a better solution. If there is one Id love to learn and if not I wonder if something like my solution can be integrated into the oauth2client code?

thanks,
Yoav

Metadata

Metadata

Labels

🚨This issue needs some love.triage meI really want to be triaged.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions