1234567891011121314151617181920212223242526272829303132333435 |
- import { Client, auth } from 'twitter-api-sdk'
- import { X_BOT_CLIENT_ID, X_BOT_CLIENT_SECRET } from '../../constants'
- import { Constant } from '../../db/models'
- // Initialize auth client first
- let authClient: auth.OAuth2User
- export async function getAuthClient(): Promise<auth.OAuth2User> {
- if (authClient) return authClient
- const token = await Constant.get('xBotToken')
- if (token) {
- authClient = new auth.OAuth2User({
- client_id: X_BOT_CLIENT_ID,
- client_secret: X_BOT_CLIENT_SECRET,
- callback: 'http://localhost:8089/api/v1/x/callback',
- scopes: ['tweet.read', 'users.read', 'offline.access'],
- token,
- })
- } else {
- authClient = new auth.OAuth2User({
- client_id: X_BOT_CLIENT_ID,
- client_secret: X_BOT_CLIENT_SECRET,
- callback: 'http://localhost:8089/api/v1/x/callback',
- scopes: ['tweet.read', 'users.read', 'offline.access'],
- })
- }
- return authClient
- }
- export async function getTwitterClient(): Promise<Client> {
- const authClient = await getAuthClient()
- return new Client(authClient)
- }
|