亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 fetch 與 Passport 結合使用會得到“請求的資源上不存在

將 fetch 與 Passport 結合使用會得到“請求的資源上不存在

精慕HU 2023-02-17 10:28:23
我想在我的網站上放置一個按鈕,讓用戶授權訪問他的 YouTube 帳戶。我正在使用 Passport 進行身份驗證。我的代碼使用 fetch 來發送請求。當我點擊按鈕時,我得到:訪問“https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fgooglecb&scope=https%3A%2F%2Fwww.googleapis .com%2Fauth%2Fyoutube&client_id=...'(從'http://localhost:3000/google'重定向)來自'http://localhost:3000'已被CORS策略阻止:對預檢請求的響應不'未通過訪問控制檢查:請求的資源上不存在“Access-Control-Allow-Origin”標頭。如果不透明的響應滿足您的需求,請將請求的模式設置為“no-cors”以在禁用 CORS 的情況下獲取資源。如果我對 /google 使用 href,它就可以工作。應用程序.js:const appConfig = require('./config.js');const cors = require('cors')const express = require('express');const passport = require('passport');const path = require('path');const YoutubeV3Strategy = require('passport-youtube-v3').Strategy;const index = require('./routes/index');const app = express();app.set('views', path.join(__dirname, 'views'));app.set('view engine', 'ejs');app.use(cors());app.use(passport.initialize());app.use(passport.session());passport.use(new YoutubeV3Strategy({  clientID: appConfig.youtube.id,  clientSecret: appConfig.youtube.secret,  callbackURL: 'http://localhost:3000/googlecb',  scope: ['https://www.googleapis.com/auth/youtube'],},function (accessToken, refreshToken, profile, cb) {  const user = {    accessToken: accessToken,    refreshToken: refreshToken,    profile: profile,  };  return cb(null, user);}));passport.serializeUser((user, cb) => {  cb(null, user);});passport.deserializeUser((obj, cb) => {  cb(null, obj);});app.use('/', index);app.use(function(req, res, next) {  var err = new Error('Not Found');  err.status = 404;  next(err);});app.use(function(err, req, res, next) {  res.locals.message = err.message;  res.locals.error = req.app.get('env') === 'development' ? err : {};  res.status(err.status || 500);  res.render('error');});module.exports = app;
查看完整描述

2 回答

?
滄海一幻覺

TA貢獻1824條經驗 獲得超5個贊

查看完整回答
反對 回復 2023-02-17
?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

解決方案是為此使用帶有后端路由 url 的錨標記。就像你已經this.router.get("/auth/google", passport.authenticate("google", { scope: ["email", "profile"], successRedirect: "http://localhost:3000" }));<a href="localhost:3000/auth/google>Click me</a>



查看完整回答
反對 回復 2023-02-17
  • 2 回答
  • 0 關注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號