我需要通过
authCookie
为了表达响应,我使用JSONP技术绕过谷歌的CORS,但我不知道如何将cookie传递给响应。所以客户端浏览器可以获取该cookie。最终的结果是直接的谷歌驱动器链接与cookie。目前只通过直接链接但cookie为空。我不知道。
客户端html
<video id="video-player" controls></video>
<script>
const videoPlayer = document.getElementById('video-player');
(async () => {
const response = await fetch('http://127.0.0.1:8080/video');
videoPlayer.src = await response.text();
})();
</script>
服务器
const express = require('express');
const htmlParser = require('node-html-parser');
const { parse } = htmlParser;
const cors = require('cors');
const fetch = require('node-fetch');
const app = express();
const port = 8080;
app.get('/video', cors(), async (req, ress) => {
const response = await fetch('https://drive.google.com/uc?id=17OunL0rN-
7jrsNGjznQTqnB_z7xfsgKK&export=download', { redirect: 'manual' });
const dom = parse(await response.text());
const url = 'https://drive.google.com' + dom.querySelector('#uc-download-link')._attrs.href;
const resCookie = await response.headers.get('set-cookie').split(';');
const downloadCookie = resCookie[0];
const nidCookie = resCookie[6].split(',')[1];
const reqCookie = downloadCookie + ';' + nidCookie;
let options = {
headers: {
cookie: reqCookie
},
redirect: 'manual'
};
const res = await fetch(url, options);
const re = await fetch(await res.headers.get('location'), {redirect: 'manual'});
const authCookie = await re.headers.get('set-cookie').split(';')[0];
const r = await fetch(await re.headers.get('location'), { headers: { cookie: nidCookie }, redirect: 'manual' });
const data = await fetch(await r.headers.get('location'), { headers: { cookie: authCookie}, redirect: 'manual'});
ress.send(await r.headers.get('location'));
});
app.listen(port , () => {
console.log(`Server is listening on ${port}`);
});
您可以使用res.cookie(名称、值(设置带有express的cookie。你可以在这里阅读更多关于它的信息。