Open umerrejolut opened 3 months ago
`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, }); }
${Config.BACKEND_URL}/account-verification/twitter/callback
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?
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
`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?