0

我正在尝试使用 google 登录到我的应用程序,但它显示如下所示的错误,

XMLHttpRequest cannot load https://accounts.google.com/o/oauth2/v2/auth?response_type=code&redirect_ur…d=410427634474-u3tpasmj4r80s6v20o54s85fikhotl79.apps.googleusercontent.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

我已经尝试过类似下面的东西

应用程序.js

passport.use(new GoogleStrategy({
    clientID        : config.googleAuth.clientID,
    clientSecret    : config.googleAuth.clientSecret,
    callbackURL     : config.googleAuth.callbackURL
},
function(accessToken, refreshToken, profile, done) {
   User.findOrCreate({ googleId: profile.id }, function (err, user) {
     return done(err, user);
   });
}
));


router.get('/pages/auth/loginWithGoogle',
    passport.authenticate('google', { scope: ['https://www.googleapis.com/auth/plus.login'] }));

router.get('/auth/google/callback',
passport.authenticate('google', {
   successRedirect : '/invite-friends',
   failureRedirect : '/pages/auth/login'
}));
4

1 回答 1

1

$http()它将发送一个 ajax 请求,这将不起作用

下面的代码在新窗口中打开 url,然后如果用户授权并且一切正常,它将重定向主页到/invite-friends

    vm.loginWithGoogle = function(){
        var win = window.open('/api/pages/auth/loginWithGoogle', "windowname1", 'width=800, height=600'); 
        var REDIRECT = 'callback';

        var pollTimer   =   window.setInterval(function() { 
            try {
                if (win.document.URL.indexOf(REDIRECT) != -1) {
                    window.clearInterval(pollTimer);
                    window.location = window.location.origin + '/invite-friends';
                    win.close();
                }
            } catch(e) {
            }
        }, 100);
    }

或使用它在当前页面中打开 url:

vm.loginWithGoogle = function(){
    location.href = '/api/pages/auth/loginWithGoogle';
}
于 2016-07-28T08:03:14.790 回答