first commit
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
// x-poster.controller.ts
|
||||
import {Body, Controller, Get, HttpException, Post, Query, Req} from '@nestjs/common';
|
||||
import {CreateTweetDto, ReplyTweetDto} from './dto/create-tweet.dto';
|
||||
import {XPosterRouterService, XStrategy} from "./x-poster.router.service";
|
||||
import {XCookieService} from "./x-cookie.service";
|
||||
import {XApiService} from "./x-api.service";
|
||||
import {XCacheService} from "../x-cache/x-cache.service";
|
||||
import {XBrowserService} from "./x-browser.service";
|
||||
|
||||
@Controller('')
|
||||
export class XPosterController {
|
||||
constructor(
|
||||
private readonly service: XPosterRouterService,
|
||||
private readonly xCookieService: XCookieService,
|
||||
private readonly xBrowserService: XBrowserService,
|
||||
private readonly xCacheService: XCacheService,
|
||||
private readonly xApiService: XApiService
|
||||
) {
|
||||
}
|
||||
|
||||
|
||||
@Get('tw_callback')
|
||||
async twitterAuthCallback(
|
||||
@Query('code') code: string,
|
||||
@Query('state') state: string,
|
||||
@Req() request: Request) {
|
||||
|
||||
console.log('twitterAuthCallback==>')
|
||||
const cacheAuthData = await this.xCacheService.getCacheTwAuthorize();
|
||||
console.log({cacheAuthData});
|
||||
//@ts-ignore
|
||||
const {codeVerifier, state: sessionState} = cacheAuthData || {}
|
||||
|
||||
if (!codeVerifier || !state || !sessionState || !code) {
|
||||
throw new HttpException('You denied the app or your session expired!', 400)
|
||||
}
|
||||
if (state !== sessionState) {
|
||||
throw new HttpException('Stored tokens didnt match!!', 400)
|
||||
}
|
||||
|
||||
const client = await this.xApiService.getTwitterClientV2();
|
||||
|
||||
const {client: loggedClient, accessToken, refreshToken} = await client.loginWithOAuth2({
|
||||
code,
|
||||
codeVerifier: codeVerifier,
|
||||
redirectUri: `http://localhost:${process.env.PORT}/tw_callback`
|
||||
});
|
||||
|
||||
await this.xCacheService.setCacheTwAccessToken(accessToken);
|
||||
await this.xCacheService.setCacheTwRefreshToken('' + refreshToken);
|
||||
console.log({loggedClient, accessToken, refreshToken});
|
||||
// @ts-ignore
|
||||
const {data: userObject} = loggedClient.v2.me();
|
||||
|
||||
return userObject;
|
||||
|
||||
}
|
||||
|
||||
@Get('tw_authorize')
|
||||
async twitterAuthorize() {
|
||||
console.log('twitterAuthorize==>', await this.xCacheService.getCacheTwAuthorize());
|
||||
|
||||
const client = await this.xApiService.getTwitterClientV2();
|
||||
const {
|
||||
url,
|
||||
codeVerifier,
|
||||
state
|
||||
} = client.generateOAuth2AuthLink(`http://localhost:${process.env.PORT}/tw_callback`,
|
||||
{
|
||||
scope:
|
||||
[
|
||||
'tweet.read',
|
||||
'tweet.write',
|
||||
'users.read',
|
||||
'offline.access',
|
||||
'media.write',
|
||||
|
||||
]
|
||||
});
|
||||
// Redirect your client to {url}
|
||||
console.log('Please go to', url);
|
||||
console.log({codeVerifier, state});
|
||||
await this.xCacheService.setCacheTwAuthorize({codeVerifier, state});
|
||||
|
||||
return {url, codeVerifier, state};
|
||||
}
|
||||
|
||||
|
||||
@Get('/tw/me')
|
||||
async testTwitterMe() {
|
||||
const client = await this.xApiService.getTwitterClientV2ViaAccessToken();
|
||||
return client.v2.me();
|
||||
}
|
||||
|
||||
|
||||
@Post('xreply')
|
||||
async xreply(
|
||||
@Body() tweet: ReplyTweetDto,
|
||||
) {
|
||||
const account = {
|
||||
accountId: process.env.X_USERNAME!,
|
||||
cookies: [
|
||||
{
|
||||
name: 'auth_token',
|
||||
value: process.env.X_COOKIE_AUTH_TOKEN!,
|
||||
},
|
||||
{
|
||||
name: 'ct0',
|
||||
value: process.env.X_COOKIE_CT0!,
|
||||
},
|
||||
],
|
||||
proxy: '',
|
||||
userAgent: ''
|
||||
};
|
||||
return this.xBrowserService.postReply(account, tweet.tweetUrl, tweet.text)
|
||||
}
|
||||
|
||||
@Post('tweet')
|
||||
async tweet(
|
||||
@Body() tweet: CreateTweetDto,
|
||||
) {
|
||||
const authToken = 'f5574950a0d98cf49ca2e574cc6a4139cc8a2d81';
|
||||
const ct0 = '75db96b40f4814925670722e3335840467b87c7eb403aab14fbe719297bf465347810f92dc2116c3d30f15153a332976c50d856983dfe19046d4f10413b3d1cd8bac6f7a99e5b03c6949bafdad0f01c0';
|
||||
// return this.service.postTweet(
|
||||
// {
|
||||
// account: {
|
||||
// id: 'realflashkaze',
|
||||
// browser: {
|
||||
// accountId: 'realflashkaze',
|
||||
// cookies: [
|
||||
// {
|
||||
// name: "ct0",
|
||||
// value: '75db96b40f4814925670722e3335840467b87c7eb403aab14fbe719297bf465347810f92dc2116c3d30f15153a332976c50d856983dfe19046d4f10413b3d1cd8bac6f7a99e5b03c6949bafdad0f01c0'
|
||||
// },
|
||||
// {
|
||||
// name: "auth_token",
|
||||
// value: 'f5574950a0d98cf49ca2e574cc6a4139cc8a2d81'
|
||||
// },
|
||||
// {name: "dnt", value: "1"},
|
||||
// {name: "lang", value: "en"},
|
||||
// {name: "twid", value: "u%3D2043937828644536320"},
|
||||
// ]
|
||||
// }
|
||||
// },
|
||||
// text: tweet.text,
|
||||
// strategy: XStrategy.BROWSER_ONLY
|
||||
// });
|
||||
}
|
||||
|
||||
@Get('verify')
|
||||
verify() {
|
||||
const account = {
|
||||
authToken: process.env.X_COOKIE_AUTH_TOKEN!, // auth_token cookie
|
||||
ct0: process.env.X_COOKIE_CT0!,
|
||||
}
|
||||
return this.xCookieService.verifyCookie(account);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user