Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
umerrejolut opened this issue Jun 7, 2024 · 1 comment

Comments

@umerrejolut
Copy link

umerrejolut commented Jun 7, 2024

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
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants