A
A
alaskafx2021-08-28 19:41:42
Node.js
alaskafx, 2021-08-28 19:41:42

How to solve 404 CORS with VK API?

I get this error:
Access to XMLHttpRequest at ' https://oauth.vk.com/authorize?response_type=code& ...' (redirected from ' localhost:3000/auth/vkontakte ') from origin ' localhost:8080 ' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Server request:

axios.get(api).then((response) => {
  console.log(response.data)
})


Server code:
const passport = require('passport');
const VKontakteStrategy = require('passport-vkontakte').Strategy;


app.use(require("cookie-parser")());
app.use(require("body-parser").urlencoded({ extended: true }));
app.use(
    require("express-session")({
        secret: "keyboard cat",
        resave: true,
        saveUninitialized: true,
    })
);
app.use(passport.initialize());
app.use(passport.session());

passport.use(
    new VKontakteStrategy(
        {
            clientID: 7937563, // VK.com docs call it 'API ID', 'app_id', 'api_id', 'client_id' or 'apiId'
            clientSecret: "тут типа секретного ключа",
            callbackURL: "http://localhost:3000/auth/vkontakte/callback",
        },
        function myVerifyCallbackFn(
            accessToken,
            refreshToken,
            params,
            profile,
            done
        ) {
            // Now that we have user's `profile` as seen by VK, we can
            // use it to find corresponding database records on our side.
            // Also we have user's `params` that contains email address (if set in
            // scope), token lifetime, etc.
            // Here, we have a hypothetical `User` class which does what it says.
            User.findOrCreate({ vkontakteId: profile.id })
                .then(function (user) {
                    done(null, user);
                })
                .catch(done);
        }
    )
);

// User session support for our hypothetical `user` objects.
passport.serializeUser(function (user, done) {
    done(null, user.id);
});

passport.deserializeUser(function (id, done) {
    User.findById(id)
        .then(function (user) {
            done(null, user);
        })
        .catch(done);
});


app.get("/auth/vkontakte", passport.authenticate("vkontakte"));

app.get(
    "/auth/vkontakte/callback",
    passport.authenticate("vkontakte", {
        successRedirect: "/",
        failureRedirect: "/login",
    })
);

app.get("/", function (req, res) {
    //Here you have an access to req.user
    res.json(req.user);
});


Tried JSONP but gives 401 error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
veryoriginalnickname, 2021-08-28
@veryoriginalnickname

you can try setting the Access-Control-Allow-Origin header on localhost:3000 to "*" or to "localhost:8080". Although I do not remember if it is possible to specify the port. Better all the same on "*".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question