superfaceai / passport-twitter-oauth2

Twitter OAuth 2.0 Strategy for Passport for accessing Twitter API v2
MIT License
28 stars 9 forks source link

Encountering a 401 Unauthorized error while trying to authenticate on Twitter. #46

Open umerrejolut opened 3 months ago

umerrejolut commented 3 months ago

Screenshot 2024-06-07 at 11 36 45 PM `import { Injectable } from '@nestjs/common'; import { PassportStrategy } from '@nestjs/passport'; import { Strategy } from '@superfaceai/passport-twitter-oauth2'; import { Config } from 'src/utils/config';

@Injectable() export class TwitterStrategy extends PassportStrategy( Strategy, 'twitter-strategy', ) { constructor() { super({ clientID: Config.TWITTER_CLIENT_ID, clientSecret: Config.TWITTER_CLIENT_SECRET, clientType: 'public', callbackURL: ${Config.BACKEND_URL}/account-verification/twitter/callback, scope: ['users.read', 'offline.access', 'tweet.read'], passReqToCallback: true, state: true, }); }

async validate( req: any, accessToken: string, refreshToken: string, profile: any, done: any, ) { const { id, username } = profile; const twitter: any = {}; twitter.twitterId = id; twitter.username = username; done(null, twitter); } async authenticate(req: any, options?: any) { try { return super.authenticate(req, options); } catch (error) { if (error.statusCode === 401) { console.log('Unauthorized') } throw error; } } } `

I attempted different approaches to handle the 401 error, but none were successful. Could you guide me on how to effectively manage this issue in the provided code snippet?

janhalama commented 3 months ago

Hi, this is caused by either misconfigured Twitter app or wrong credentials. Did you go through the tutorial?

I suggest you to use one of our example apps with your Twitter app & credentials.

Jan