Note that there are some explanatory texts on larger screens.

plurals
  1. POFollowing redirects in Node.js
    primarykey
    data
    text
    <p>I'm working on proxy for downloading files from Google Docs. Since the download urls Google Drive API gives for non-gdrive filetypes (pdf, txt, jpeg) are redirects and having read <a href="https://stackoverflow.com/questions/7323932/how-do-you-follow-an-http-redirect-in-node-js">[1]</a> and <a href="https://stackoverflow.com/questions/11061118/node-js-requests-returning-301-redirects">[2]</a>, I decided to use <a href="https://github.com/olalonde/follow-redirects" rel="nofollow noreferrer">follow-redirects library</a>. So when you hit <code>http://localhost:3000/oauth2_download?accessToken=x&amp;url=my_url</code> where x is my GDrive access_token, url is, say, <code>https://docs.google.com/uc?id=0B_31K_MP92hUNjljYjIyMzgtZTBmNS00MGMwLWIxNmQtYjMyNDFiYjY0MTJl&amp;export=download</code>, the file is supposed to be downloaded. However, the browser sends me to the Google Docs sign-in page which (given that hitting <code>https://docs.google.com/uc?id=0B_31K_MP92hUNjljYjIyMzgtZTBmNS00MGMwLWIxNmQtYjMyNDFiYjY0MTJl&amp;export=download</code> in browser downloads the file if you're logged into Google Docs) means the headers are not being sent correctly. Any idea how should I use 'follow-redirects' library to deal with this issue? Thank you in advance!</p> <pre><code>//var https = require('https'); var https = require('follow-redirects').https; var app = require('express')(); var urllib = require('url'); // Downloads the file using OAuth2. Tested on Google Drive app.get('/oauth2_download', function(req, res){ var url = req.param('url'); var accessToken = req.param('accessToken'); var headers = { "Authorization": "Bearer " + accessToken } var searchname = urllib.parse(url).search; var hostname = urllib.parse(url).hostname; var pathname = urllib.parse(url).pathname; if (url &amp;&amp; accessToken) { // downloading file with streaming var options = { hostname: hostname, port: 443, path: pathname + searchname, method: 'GET', headers: headers }; https.request(options, function(response){ for (var key in response.headers) { res.setHeader(key, response.headers[key]); } response.on('data', function(chunk) { res.write(chunk); }); response.on('end', function() { res.end(); }); //console.log("Done downloading using OAuth2"); }).on('error', function(e) { console.error(e); }).end(); } else { res.send(404); res.end(); } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload