Skip to content

Commit c1d34a8

Browse files
committed
add example
Signed-off-by: ふぁ <yuki@yuki0311.com>
1 parent 17d32cb commit c1d34a8

File tree

8 files changed

+3239
-0
lines changed

8 files changed

+3239
-0
lines changed

example/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.env

example/.prettierrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"singleQuote": true,
3+
"semi": true,
4+
"bracketSpacing": true,
5+
"arrowParens": "always",
6+
"printWidth": 120,
7+
"trailingComma": "all"
8+
}

example/package-lock.json

Lines changed: 3122 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "example",
3+
"license": "MIT",
4+
"version": "1.0.0",
5+
"author": "",
6+
"description": "",
7+
"scripts": {
8+
"start": "npx ts-node -r tsconfig-paths/register src/main.ts"
9+
},
10+
"dependencies": {
11+
"twitter-api-v2": "^1.15.2",
12+
"twitter-openapi-typescript": "^0.0.19"
13+
},
14+
"devDependencies": {
15+
"@types/node": "*",
16+
"@types/node-fetch": "*",
17+
"@typescript-eslint/eslint-plugin": "*",
18+
"@typescript-eslint/parser": "*",
19+
"dotenv": "^16.3.0",
20+
"eslint": "^8.50.0",
21+
"eslint-config-standard-with-typescript": "*",
22+
"eslint-plugin-import": "*",
23+
"eslint-plugin-n": "*",
24+
"eslint-plugin-promise": "*",
25+
"log4js": "^6.9.0",
26+
"node-fetch": "^2.0.0",
27+
"typescript": "^5.2.0"
28+
}
29+
}

example/src/login.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { TwitterOpenApi } from 'twitter-openapi-typescript';
2+
import { TwitterApi, ITwitterApiClientPlugin } from 'twitter-api-v2';
3+
import fetch from 'node-fetch';
4+
5+
import * as dotenv from 'dotenv';
6+
dotenv.config();
7+
8+
const authToken = process.env.AUTH_TOKEN as string;
9+
const CsrfToken = process.env.CSRF_TOKEN as string;
10+
11+
export const login = async () => {
12+
const cookie = { auth_token: authToken, ct0: CsrfToken };
13+
14+
const api = new TwitterOpenApi({ fetchApi: fetch as any });
15+
const client = await api.getClientFromCookies(cookie);
16+
17+
const plugin: ITwitterApiClientPlugin = {
18+
onBeforeRequest: async (params) => {
19+
params.computedParams.headers = {
20+
...params.computedParams.headers,
21+
...TwitterOpenApi.api_key,
22+
'x-csrf-token': cookie.ct0,
23+
'x-twitter-auth-type': 'OAuth2Session',
24+
authorization: `Bearer ${TwitterOpenApi.bearer}`,
25+
cookie: api.cookieEncode(cookie),
26+
};
27+
params.requestOptions.headers = {
28+
...params.requestOptions.headers,
29+
...TwitterOpenApi.api_key,
30+
'x-csrf-token': cookie.ct0,
31+
'x-twitter-auth-type': 'OAuth2Session',
32+
authorization: `Bearer ${TwitterOpenApi.bearer}`,
33+
cookie: api.cookieEncode(cookie),
34+
};
35+
},
36+
};
37+
38+
const legacy = new TwitterApi('_', { plugins: [plugin] });
39+
40+
return { client, legacy };
41+
};
42+
43+
export default login;

example/src/tweetMedia.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import login from '@/login';
2+
3+
const main = async () => {
4+
const { client, legacy } = await login();
5+
6+
const data = await legacy.v1.uploadMedia('test.png');
7+
8+
client.getPostApi().postCreateTweet({
9+
tweetText: 'Hello World!!',
10+
mediaIds: [data],
11+
});
12+
};
13+
14+
main();

example/test.png

447 Bytes
Loading

example/tsconfig.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"target": "es5",
5+
"downlevelIteration": true,
6+
"module": "commonjs",
7+
"moduleResolution": "node",
8+
"outDir": "dist",
9+
"lib": ["es2020", "dom"],
10+
"typeRoots": ["node_modules/@types"],
11+
"allowSyntheticDefaultImports": true,
12+
"rootDir": "./",
13+
"baseUrl": "./",
14+
"paths": {
15+
"@/*": ["./src/*"]
16+
},
17+
"declarationMap": true,
18+
"sourceMap": true,
19+
"strict": true
20+
}
21+
}

0 commit comments

Comments
 (0)