错误:禁止访问 您的客户端没有权限从此服务器获取URL /twitter/sucess。

13 浏览
0 Comments

错误:禁止访问 您的客户端没有权限从此服务器获取URL /twitter/sucess。

我正在使用Node.js API进行Twitter身份验证。我已经在Firebase Functions上部署了Node.js API。我还阅读了关于IMA的文档,并在Google Cloud控制台中设置了allUsers角色。如果我尝试使用/hello端点,那么我的代码可以正常工作,并且我可以在屏幕上看到结果。但是当我使用res.redirect()时,我遇到了以下错误:\n错误:Forbidden 您的客户端没有权限获取URL /twitter/login。\n

const functions = require("firebase-functions");
var express = require('express');
var path = require('path');
// var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var passport = require('passport');
var Strategy = require('passport-twitter').Strategy;
var session = require('express-session');
const firebase = require('firebase');
const port = 3000
const firebaseConfig = {
    apiKey: "",
    authDomain: "",
    projectId: "",
    storageBucket: "",
    messagingSenderId: "",
    appId: ""
};
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
const Users = db.collection("users")
passport.use(new Strategy({
    consumerKey: '',
    consumerSecret: '',
    callbackURL: ''
}, function (token, tokenSecret, profile, callback) {
    return callback(null, profile);
}));
passport.serializeUser(function (user, callback) {
    callback(null, user);
})
passport.deserializeUser(function (obj, callback) {
    callback(null, obj);
})
var app = express();
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({ secret: 'whatever', resave: true, saveUninitialized: true }))
app.use(passport.initialize())
app.use(passport.session())
app.get('/', function (req, res) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET,PUT,PATCH,POST,DELETE");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    if (req.user == undefined) {
        // res.json({"message": "All ok"})
        res.redirect('/twitter/login')
    }
    else if (req.user != undefined) {
        return;
    }
})
app.get('/hello', function (req, res) {
    res.json({"message": "Hello"})
})
app.get('/twitter/login', passport.authenticate('twitter', {
    failureRedirect: '/'
}))
app.get('/twitter/sucess', function (req, res) {
    console.log("in twitter success")
    console.log(req.user.username);
    console.log(req.user);
    res.cookie("twitter_id", req.user.id, { expires: new Date(Date.now() + (7300 * 24 * 3600000)) });
    const userRef = db.collection("users").doc(req.user.id)
    userRef.get()
        .then((docSnapshot) => {
            if (docSnapshot.exists) {
            } else {
                userRef.set({"userName": req.user.username, "twitterId": req.user.id, "displayName": req.user.displayName, "crushCount": 0}) // create the document
            }
        });
    res.redirect("http://localhost:8000/add_crush");
    // res.status(200).json({"message": "Auth Successfull", "username": req.user.username})
})
app.get('/twitter/return', passport.authenticate('twitter', {
    failureRedirect: '/',
    successRedirect: '/twitter/sucess',
}), function (req, res) {
})
app.listen(port, () => console.log(`Listening on port ${port}`))
exports.app = functions.https.onRequest(app)

\n这是我的代码,请建议该怎么办?

0
0 Comments

问题的原因是:身份验证方式不正确。

解决方法可以参考以下社区帖子:123,了解更多修复错误的替代方法。

你可以查阅这个使用passport.js进行Node.js Twitter登录脚本的文档

0