-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlogin.ts
42 lines (34 loc) · 1.28 KB
/
login.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { ITwitterApiClientPlugin, TwitterApi } from 'twitter-api-v2';
import { TwitterOpenApi } from 'twitter-openapi-typescript';
import * as dotenv from 'dotenv';
dotenv.config();
const authToken = process.env.AUTH_TOKEN as string;
const CsrfToken = process.env.CSRF_TOKEN as string;
export const login = async () => {
const cookie = { auth_token: authToken, ct0: CsrfToken };
const api = new TwitterOpenApi();
const client = await api.getClientFromCookies(cookie);
const plugin: ITwitterApiClientPlugin = {
onBeforeRequest: async (params) => {
params.computedParams.headers = {
...params.computedParams.headers,
...TwitterOpenApi.api_key,
'x-csrf-token': cookie.ct0,
'x-twitter-auth-type': 'OAuth2Session',
authorization: `Bearer ${TwitterOpenApi.bearer}`,
cookie: api.cookieEncode(cookie),
};
params.requestOptions.headers = {
...params.requestOptions.headers,
...TwitterOpenApi.api_key,
'x-csrf-token': cookie.ct0,
'x-twitter-auth-type': 'OAuth2Session',
authorization: `Bearer ${TwitterOpenApi.bearer}`,
cookie: api.cookieEncode(cookie),
};
},
};
const legacy = new TwitterApi('_', { plugins: [plugin] });
return { client, legacy };
};
export default login;