I’m working with Puppeteer and trying to avoid the Twitter login process by using previously saved cookies. When I extract cookies from the browser console using document.cookie and try to apply them, I keep getting an error message saying Invalid parameters name: string value expected.
What’s the correct way to set these cookies so Twitter recognizes the session as already authenticated? Am I missing something in the cookie format or the method I’m using?
jack’s right about the format. but grab fresh cookies and set them before you navigate to twitter - don’t use old ones. twitter tokens expire fast, so i always set cookies immediately after getting them.
Your problem is that you’re passing an object to setCookie instead of the correct format. Puppeteer’s setCookie requires each cookie to be a separate object with name, value, domain, and path. I’ve encountered a similar issue with Twitter automation before.
You should structure it as follows:
await page.setCookie(
{ name: 'user_pref_id', value: 'v1_ABC123def+XYZqwerty789==', domain: '.twitter.com' },
{ name: 'visitor_token', value: 'v1%3A987654321012345678', domain: '.twitter.com' },
{ name: 'csrf_token', value: 'a1b2c3d4e5f6789012345678901234ab', domain: '.twitter.com' }
// continue adding other cookies in the same format
);
Make sure to set the cookies first and then navigate to Twitter. The domain is crucial as Twitter uses both ‘.twitter.com’ and ‘twitter.com’, depending on the specific cookie. Use the developer tools to verify which domain each cookie should be associated with.
Watch out for the secure flag and sameSite attributes when setting Twitter cookies. I had auth issues until I figured out some cookies need secure=true. When you grab cookies from browser console, you’re only getting name-value pairs - you’re missing the important stuff like httpOnly, secure, and sameSite settings. Twitter’s really picky about these security attributes. Use browser.cookies() instead of document.cookie to get the complete cookie data with all flags, then recreate them exactly. Also make sure you’re setting cookies on both twitter.com and x.com domains since Twitter bounces between them now.