Skip to content

Commit 4afe8ef

Browse files
committed
update guest-token
Signed-off-by: ふぁ <yuki@yuki0311.com>
1 parent 1e443b6 commit 4afe8ef

File tree

5 files changed

+52
-28
lines changed

5 files changed

+52
-28
lines changed
Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,37 @@
1-
# twitter-openapi-typescript@0.0.5
1+
# twitter-openapi-typescript
2+
3+
## Setup
24

35
```sh
46
npm i twitter-openapi-typescript
57
```
68

9+
## Usage
10+
711
```typescript
812
import { TwitterOpenApi } from "twitter-openapi-typescript";
9-
import fetch from "node-fetch";
1013

1114
const api = new TwitterOpenApi();
12-
api.setFetchApi(fetch as any);
13-
const client = await api.getClientFromCookies('<csrf toke here>', '<auth token here>'); // option
15+
const client = await api.getClient();
1416
const response = await client.getUserApi().getUserByScreenName({ screenName: 'elonmusk' });
1517
console.log(response.data.legacy.screenName);
1618
console.log(`followCount: ${response.data.legacy.friendsCount} followersCount: ${response.data.legacy.followersCount}`);
1719
```
20+
21+
### Node.js
22+
23+
```typescript
24+
import fetch from "node-fetch";
25+
const api = new TwitterOpenApi({ fetchApi: fetch as any });
26+
```
27+
28+
### Login
29+
30+
```typescript
31+
const client = await api.getClientFromCookies('<csrf toke here>', '<auth token here>');
32+
```
33+
34+
### List of APIs
35+
36+
You should read the Test case.
37+
<https://github.com/fa0311/twitter-openapi-typescript/tree/master/twitter-openapi-typescript/test/api>

twitter-openapi-typescript/package-lock.json

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

twitter-openapi-typescript/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "twitter-openapi-typescript",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "Implementation of Twitter internal API in TypeScript",
55
"scripts": {
66
"test": "jest",
@@ -21,7 +21,7 @@
2121
"main": "./dist/src/index.js",
2222
"typings": "./dist/src/index.d.ts",
2323
"dependencies": {
24-
"twitter-openapi-typescript-generated": "^0.0.3"
24+
"twitter-openapi-typescript-generated": "^0.0.5"
2525
},
2626
"devDependencies": {
2727
"@types/jest": "^28.0.0",

twitter-openapi-typescript/src/api.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,26 @@ export class TwitterOpenApi {
6060
const response = await this.fetchApi(TwitterOpenApi.twitter, { method: 'GET', redirect: 'manual' });
6161
const chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
6262
const csrfToken = [...Array(32)].reduce((a) => a + chars[Math.floor(Math.random() * chars.length)], '');
63-
const cookie = this.cookieDecode(response.headers.get('set-cookie')!);
64-
cookie.push({ name: 'ct0', value: csrfToken });
63+
const cookies = this.cookieDecode(response.headers.get('set-cookie')!);
64+
cookies.push({ name: 'ct0', value: csrfToken });
6565

6666
const html = await this.fetchApi(TwitterOpenApi.twitter, {
6767
method: 'GET',
68-
headers: { Cookie: this.cookieEncode(cookie) },
68+
headers: { Cookie: this.cookieEncode(cookies) },
6969
}).then((response) => response.text());
7070

7171
const re = new RegExp('<script nonce="([a-zA-Z0-9]{48})">(document.cookie="(.*?)";)+<\\/script>');
7272

7373
const script = html.match(re)[0];
74-
const document = script
74+
script
7575
.split('document.cookie="')
7676
.slice(1)
7777
.map((e) => e.replace('</script>', '').replace('";', ''))
7878
.map((e) => this.cookieDecode(e)[0])
79-
.filter((e) => cookie.findIndex((c) => c.name == e.name) == -1)
80-
.forEach((e) => cookie.push(e));
79+
.filter((e) => cookies.findIndex((c) => c.name == e.name) == -1)
80+
.forEach((e) => cookies.push(e));
8181

82-
return cookie;
82+
return cookies;
8383
}
8484

8585
setCookies(context: i.RequestContext, cookies: TwitterOpenApiCookie[]): i.RequestContext {
@@ -101,6 +101,7 @@ export class TwitterOpenApi {
101101
'x-twitter-client-language': this.param.lang ?? 'en',
102102
'x-twitter-active-user': 'yes',
103103
'x-csrf-token': cookies.find((cookie) => cookie.name === 'ct0')?.value,
104+
'x-guest-token': cookies.find((cookie) => cookie.name === 'gt')?.value,
104105
}[key]!;
105106
},
106107
accessToken: TwitterOpenApi.bearer,
@@ -110,11 +111,14 @@ export class TwitterOpenApi {
110111
}
111112

112113
async getClientFromCookies(ct0: string, authToken: string): Promise<TwitterOpenApiClient> {
113-
const cookies: TwitterOpenApiCookie[] = [
114-
...(await this.getGuestSession()),
114+
const cookies: TwitterOpenApiCookie[] = await this.getGuestSession();
115+
[
115116
{ name: 'auth_token', value: authToken },
116117
{ name: 'ct0', value: ct0 },
117-
];
118+
]
119+
.filter((e) => cookies.findIndex((c) => c.name == e.name) == -1)
120+
.forEach((e) => cookies.push(e));
121+
118122
const config: i.ConfigurationParameters = {
119123
fetchApi: this.param.fetchApi || fetch,
120124
middleware: [{ pre: async (context) => this.setCookies(context, cookies) }],
@@ -124,7 +128,7 @@ export class TwitterOpenApi {
124128
'x-twitter-client-language': this.param.lang ?? 'en',
125129
'x-twitter-active-user': 'yes',
126130
'x-twitter-auth-type': 'OAuth2Session',
127-
'x-csrf-token': ct0,
131+
'x-csrf-token': cookies.find((cookie) => cookie.name === 'ct0')?.value,
128132
}[key]!;
129133
},
130134
accessToken: TwitterOpenApi.bearer,

twitter-openapi-typescript/test/init.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ export const logger = log4js
2727
.getLogger('Test');
2828

2929
export const getClient = async () => {
30-
const api = new TwitterOpenApi();
31-
api.setFetchApi(fetch as any);
30+
const api = new TwitterOpenApi({ fetchApi: fetch as any });
3231
const client = await api.getClientFromCookies(ct0, authToken);
32+
// const client = await api.getClient();
3333
return client;
3434
};

0 commit comments

Comments
 (0)