N
N
neoneel2018-04-24 20:21:10
JavaScript
neoneel, 2018-04-24 20:21:10

Why does VK authentication fail?

Tell me, good people.
The bottom line is this:
-we have nodeJ
-express module
-passportJS
such a link: such a config for the passport strategy
<a href="/auth/vkontakte">click here</a>

var passport = require('passport');
var app = require('../app.js');
const VKontakteStrategy = require('passport-vkontakte').Strategy;
passport.use(new VKontakteStrategy(
  {
    clientID:     'APPID', // VK.com docs call it 'API ID', 'app_id', 'api_id', 'client_id' or 'apiId'
    clientSecret: 'APPsecret',
    callbackURL:  "http://localhost:3001/auth/vkontakte/callback",
    lang: 'ru'
  },
  function myVerifyCallbackFn(accessToken, refreshToken, params, profile, done) {
//success
console.log(accessToken);
  }
));

such a routing
app.get('/auth/vkontakte', passport.authenticate('vkontakte', { scope: ['status', 'email', 'friends', 'notify', 'wall'] }));
app.get('/auth/vkontakte/callback',
  passport.authenticate('vkontakte', {
    successRedirect: '/success',
    failureRedirect: '/login'
  })
);

The code works half for some reason ...
when you click on the link, it redirects to the VK authorization page, where it receives the code. When you click "Allow", upon obtaining rights on the authorization page, it loads for a long time, and then falls off with an error, while loading, information about the token is displayed in the console (the line with console.log () in the config)
the error is as follows:
TokenError: Code is invalid or expired.
    at Strategy.OAuth2Strategy.parseErrorResponse (/home/pavel/Documents/AgNetApp/node_modules/passport-vkontakte/node_modules/passport-oauth2/lib/strategy.js:320:12)
    at Strategy.parseErrorResponse (/home/pavel/Documents/AgNetApp/node_modules/passport-vkontakte/lib/passport-vkontakte/strategy.js:176:54)
    at Strategy.OAuth2Strategy._createOAuthError (/home/pavel/Documents/AgNetApp/node_modules/passport-vkontakte/node_modules/passport-oauth2/lib/strategy.js:367:16)
    at /home/pavel/Documents/AgNetApp/node_modules/passport-vkontakte/node_modules/passport-oauth2/lib/strategy.js:166:45
    at /home/pavel/Documents/AgNetApp/node_modules/oauth/lib/oauth2.js:191:18
    at passBackControl (/home/pavel/Documents/AgNetApp/node_modules/oauth/lib/oauth2.js:132:9)
    at IncomingMessage.<anonymous> (/home/pavel/Documents/AgNetApp/node_modules/oauth/lib/oauth2.js:157:7)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:975:12)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question