1

我想通过在 Node.js 中运行的 puppeteer 通过 Google Docs Viewer 预览文档。该 URL 在浏览器中运行良好:
https ://docs.google.com/viewer?url=https%3A%2F%2Fdocs.npmjs.com%2Fimages%2Fnpm.svg&embedded=true

但是在 puppeteer 中抛出错误:

(async () => {          
        const browser = await puppeteer.launch();
        const page = await browser.newPage();           
        page.on('response', res => {
            res.text().then(text => {
                console.log('response:', {
                    url: res.url(),
                    status: res.status(),
                    res_headers: res.headers(),
                    req_headers: res.request().headers(),
                    text: text
                });
            });
        });

        await page.setBypassCSP(true);
        await page.goto(url, {waitUntil: 'networkidle0'});
        let content = await page.content();            
        await browser.close();

        return content;
    })();

印刷事件:

网址https : //apis.google.com//scs/apps-static//js/k=oz.gapi.en_US.f5JujS1eFMY.O/m=googleapis_proxy/rt=j/sv=1/d=1/ ed=1/am=QQE/rs=AGLTcCNDI1_ftdVIpg6jNiygedEKTreQ2A/cb=gapi.loaded_0
状态:200

控制台:加载资源失败:服务器响应状态为 401 ()
urlhttps ://content.googleapis.com/drive/v2internal/about?fields=importFormats,kind&key=AIzaSyDVQw45DwoYh632gvsP5vPDqEKvb-Ywnb8
文本:{“error” : {"errors": [{"domain": "global", "reason": "required","message": "Login required","locationType": "header","location": "Authorization"}] “代码”:401,“消息”:“需要登录”}}

网址:' https://content.googleapis.com/drive/v2internal/viewerimpressions?key=AIzaSyDVQw45DwoYh632gvsP5vPDqEKvb-Ywnb8&alt=json '
状态:204

控制台:'加载资源失败:服务器响应状态为 403 ()'
url:' https://content.googleapis.com/drive/v2internal/apps?fields=items(authorized,chromeExtensionIds,createInFolderTemplate,createUrl,图标(iconUrl,size,category),id,已安装,种类,longDescription,name,objectType,openUrlTemplate,origins,primaryFileExtensions,primaryMimeTypes,productId,productUrl,rankingInfo,removable,requiresAuthorizationBeforeOpenWith,secondaryFileExtensions,secondaryMimeTypes,shortDescription,supportsCreate,supportsImport,supportsMultiOpen ,supportsTeamDrives,type,useByDefault),kind&languageCode=en-US&key=AIzaSyDVQw45DwoYh632gvsP5vPDqEKvb-Ywnb8 ',
text:{“错误”:{“错误”:[{“域”:“全局”,“原因”:“禁止”,“消息”:“禁止”}],“代码”:403,“消息”:“禁止"}},

当我比较 puppeter 和浏览器之间的 cookie 和标头时,标头“origin”、“x-origin”、“x-referrer”是相同的,但“x-chrome-connected”和“x-client-date”缺失。
cookie“NID”存在。

4

1 回答 1

1

仔细看看谷歌的回复:

{“错误”:[{“域”:“全局”,“原因”:“必需”,“消息”:“需要登录”}]}

{“错误”:[{“域”:“全局”,“原因”:“禁止”,“消息”:“禁止”}]}

puppeteer Chromium 似乎需要使用 Google 登录才能使用 PDF 查看器功能。

于 2018-06-18T13:10:38.467 回答