我正在将 fetch 从 React 发送到 Express 以通过 Google 进行身份验证,但我的访问被 CORS 错误阻止。我将 POST 请求从 React 重定向到 Google URL 进行身份验证。我尝试在 Express 应用程序中使用 cors,但仍然遇到相同的错误。 用于获取
const handleClick = (e) => {
fetch('http://localhost:8000/api/mail/login', {
method: 'POST'
})
.then(res => res.text())
}
在 Express app.js 中使用 cors
app.use(cors())
尝试重定向到 Google 身份验证
const oauth2Client = new google.auth.OAuth2(
process.env.CLIENT_ID,
process.env.CLIENT_SECRET,
process.env.REDIRECT_URI
)
const url = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: process.env.SCOPE
})
const gmail = google.gmail({
version: 'v1',
auth: oauth2Client
})
router.post('/login', (req, res, next) => {
res.redirect(url)
})
错误:访问“https://accounts.google .com/o/oauth2/v2/auth?access_type=offline&scope=https%3A%2F%2Fmail.google. com%2F&response_type=code&client_id=727520060136”进行提取-ngpfd4ll798v42gfclh7cms9ndqstt32.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8000'(从'http://localhost:8000/api/mail/login'重定向)来自原点'http://localhost:3000'已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头。如果不透明响应满足您的需求,请将请求模式设置为“no-cors”以在禁用 CORS 的情况下获取资源。
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号