Search code examples
node.jsexpresspassport.jstwitter-oauth

Getting Unable to verify authorization request state while using superfaceai / passport-twitter-oauth2


I have simply followed the example shared by superfaceai / passport-twitter-oauth2
https://github.com/superfaceai/passport-twitter-oauth2/blob/main/examples/basic-usage/server.js

When I implemented the code for OAuth 2.0 authentication, I encountered a persistent problem. Whenever I attempted to log in or authorize my application, it consistently redirected me to a designated 'failureRedirect' URL. Along with this redirection, an error message was displayed that read: 'Unable to verify authorization request state . please refer the attached image for error msg

const passport = require('passport');
const { Strategy } = require('@superfaceai/passport-twitter-oauth2');
const session = require('express-session');

const TWITTER_CLIENT_ID = "xxxxxxxxxxxxxxxxx"
const TWITTER_CLIENT_SECRET = "xxxxxxxxxxxxxxxxx"

passport.serializeUser(function (user, done) {
  done(null, user);
});
passport.deserializeUser(function (obj, done) {
  done(null, obj);
});

// Use the Twitter OAuth2 strategy within Passport
passport.use(
  new Strategy(
    {
      clientID: TWITTER_CLIENT_ID,
      clientSecret: TWITTER_CLIENT_SECRET,
      clientType: 'confidential',
      callbackURL: `http://127.0.0.1:6002/social/twitter/callback`,
    },
    (accessToken, refreshToken, profile, done) => {
      console.log('Success!', { accessToken, refreshToken });
      return done(null, profile);
    }
  )
);

const app = express();

app.use(passport.initialize());
app.use(
  session({ secret: 'keyboard cat', resave: false, saveUninitialized: true })
);

app.get(
  '/social/twitter',
  passport.authenticate('twitter', {
    scope: ['tweet.read', 'users.read', 'offline.access'],
  })
);

app.get(
  '/login',
  function (req, res) {
    const sessionData = JSON.stringify(req.session, undefined, 2);
    res.end(
      `<h1>Authentication Failed</h1> User data: <pre>${sessionData}</pre>`
    );
  }
);

app.get(
  '/social/twitter/callback',
  passport.authenticate('twitter',
  {failureRedirect:'/login',
  failureMessage:true
}
  ),
  function (req, res) {
    const userData = JSON.stringify(req.user, undefined, 2);
    res.end(
      `<h1>Authentication succeeded</h1> User data: <pre>${userData}</pre>`
    );
  }
);

app.listen(6002, () => {
  console.log(`Listening on http://127.0.0.1:6002`);
});


[![enter image description here][1]][1]
 


  *[1]: https://i.sstatic.net/rTZut.png*

Solution

  • The issue I found is, I used auth1.0 client cred on superfaceai/passport-twitter-oauth2. also, I tried this which works like a charm https://github.com/superfaceai/twitter-demo/.