fix(twitter): gt cookie

This commit is contained in:
DIYgod
2024-10-07 00:26:09 +08:00
parent a2ca21a11b
commit 6f6373932d
3 changed files with 24 additions and 8 deletions
+22 -6
View File
@@ -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
@@ -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,
@@ -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 }) => {
+1 -1
View File
@@ -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;
+1 -1
View File
@@ -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;