对 https://accounts.google .com/o/oauth2/v2/auth 的 fetch 访问已被 CORS 阻止
P粉398117857
P粉398117857 2023-11-03 22:56:28
[React讨论组]

我正在将 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 的情况下获取资源。

P粉398117857
P粉398117857

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号