Authenticate using browser cookies
mainTo avoid anti-bot protection or error 399 during login, you can use cookies exported from an authenticated browser session.
- Export Cookies:
- Chrome/Edge: Copy all cookies from the Application tab in DevTools as a semicolon-separated string (
name1=value1; name2=value2; ...). - Firefox: Find
ct0andauth_tokenin the Storage tab and construct the string:ct0=<value>; auth_token=<value>.
- Chrome/Edge: Copy all cookies from the Application tab in DevTools as a semicolon-separated string (
- Apply Cookies: Use
scraper.setCookies(cookies)wherecookiesis an array oftough-cookieCookieobjects.
import { Cookie } from 'tough-cookie';
import { Scraper } from '@the-convocation/twitter-scraper';
// Your cookie string from browser (name=value; name2=value2; ...)
const cookieString = 'ct0=abc123; auth_token=xyz789; lang=en; ...';
// Parse the cookie string
const cookies = cookieString
.split(';')
.map((c) => Cookie.parse(c))
.filter(Boolean);
// Create scraper and set cookies
const scraper = new Scraper();
await scraper.setCookies(cookies);
// Verify authentication works
const isLoggedIn = await scraper.isLoggedIn();
if (isLoggedIn) {
console.log('✓ Successfully authenticated with cookies!');
const profile = await scraper.getProfile('username');
}