0

Im new in Puppeteer and im stuck. Project is related to Angular App, on which user is logged in by link from email. I need to pass barear token to the headless browser. Whatever solution i found online, its not working. Is there any option to pass user object in Local Storage perhaps? Or any other idea?

Thanks

Marina M
  • 29
  • 5
  • How do you start the Angular/Puppeteer application? All the examples I see start the application with `ng test app` but I don't want to test it, I want to use it to generate PDFs/images at server side – ps0604 Jul 14 '20 at 12:05

3 Answers3

1

faced the same issue but tried to copy the cookies and worked with me

        var browserFetcher = new BrowserFetcher();
        await browserFetcher.DownloadAsync();
        await using var browser = await Puppeteer.LaunchAsync(
            new LaunchOptions { Headless = true,IgnoreHTTPSErrors=true });

        await using var page = await browser.NewPageAsync();

        var keys = HttpContext.Request.Cookies.Keys;

        string cookieString = "";

        foreach (var key in HttpContext.Request.Cookies.Keys) {
            cookieString  = cookieString + key +"="+ HttpContext.Request.Cookies[key]+";";
        }
        await page.SetExtraHttpHeadersAsync(new Dictionary<string, string> {
                        { "cookie" , string.Join(';',cookieString) }
                    });
msaadoun
  • 21
  • 3
0

Hello did you try with the api to modify the header?

const headers = new Map();
headers.set(
  'Authorization',
  `Basic ${new Buffer(`${myuser}:${mypass}`).toString('base64')}`
);

await page.setExtraHTTPHeaders(headers);

https://github.com/puppeteer/puppeteer/blob/v2.0.0/docs/api.md#pagesetextrahttpheadersheaders

maxshuty
  • 9,708
  • 13
  • 64
  • 77
rojaswilmer
  • 120
  • 4
0

rojaswilmer has already suggested to use setExtraHTTPHeaders. Just change Basic with Bearer since it's not HTTP Basic auth, as explained also in another question in StackOverflow.

Chris
  • 1,140
  • 15
  • 30
  • I did tried like that as well, passes in the browser, but nothing is happening. Page is not chaning to the profile one. And i have error ' Token could not be parsed from the request ' – Marina M Dec 19 '19 at 14:48