Skip to content

Commit

Permalink
fix(twitter): gt cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Oct 6, 2024
1 parent a2ca21a commit 6f63739
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
28 changes: 22 additions & 6 deletions lib/routes/twitter/api/web-api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,19 @@ const token2Cookie = (token) =>
uri: proxy.proxyUri,
})
: new CookieAgent({ cookies: { jar } });
await ofetch('https://x.com', {
dispatcher: agent,
});
if (token) {
await ofetch('https://x.com', {
dispatcher: agent,
});
} else {
const data = await ofetch('https://x.com/narendramodi?mx=2', {
dispatcher: agent,
});
const gt = data.match(/document\.cookie="gt=(\d+)/)?.[1];
if (gt) {
jar.setCookieSync(`gt=${gt}`, 'https://x.com');
}
}
return JSON.stringify(jar.serializeSync());
} catch {
// ignore
Expand Down Expand Up @@ -72,7 +82,7 @@ export const twitterGot = async (

const requestUrl = `${url}?${queryString.stringify(params)}`;

let cookie: string | Record<string, any> | null | undefined = auth?.token ? await token2Cookie(auth.token) : null;
let cookie: string | Record<string, any> | null | undefined = await token2Cookie(auth?.token);
if (!cookie && auth) {
cookie = await login({
username: auth.username,
Expand Down Expand Up @@ -129,11 +139,17 @@ export const twitterGot = async (
'content-type': 'application/json',
dnt: '1',
pragma: 'no-cache',
referer: 'https://x.com/narendramodi',
referer: 'https://x.com/',
'x-twitter-active-user': 'yes',
'x-twitter-auth-type': 'OAuth2Session',
'x-twitter-client-language': 'en',
'x-csrf-token': jsonCookie.ct0,
...(auth?.token
? {
'x-twitter-auth-type': 'OAuth2Session',
}
: {
'x-guest-token': jsonCookie.gt,
}),
},
dispatcher: dispatchers?.agent,
onResponse: async ({ response }) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/twitter/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async function handler(ctx) {
try {
data = await api.getUserMedia(id, params);
} catch (error) {
logger.debug(error);
logger.error(error);
}
const profileImageUrl = userInfo?.profile_image_url || userInfo?.profile_image_url_https;

Expand Down
2 changes: 1 addition & 1 deletion lib/routes/twitter/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async function handler(ctx) {
data = utils.excludeRetweet(data);
}
} catch (error) {
logger.debug(error);
logger.error(error);
}

const profileImageUrl = userInfo?.profile_image_url || userInfo?.profile_image_url_https;
Expand Down

0 comments on commit 6f63739

Please sign in to comment.