我有一个带有 NextJs 的 SPA,可以向 Google Sheet 提交联系表单,该表单在本地工作得很好,但在生产中却出现了 500 错误。
在我的 .env 文件中,我有以下内容:
NEXT_PUBLIC_GOOGLE_CLIENT_EMAIL= NEXT_PUBLIC_GOOGLE_PRIVATE_KEY= NEXT_PUBLIC_GOOGLE_SHEET_ID=
我的 .env.local 文件中确实有实际的秘密,
这是我的 submit.js 文件
import { google } from 'googleapis'
require('dotenv-flow').config()
export default async function handler(req, res) {
if (req.method !== 'POST') {
return res.status(405).send('Only POST requests are allowed!')
}
// log to see the secret which are visible in local
console.log('process.env', process.env)
console.log(
'email process with error ',
process.env.NEXT_PUBLIC_GOOGLE_CLIENT_EMAIL
)
const body = req.body
try {
const auth = new google.auth.GoogleAuth({
credentials: {
client_email: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_EMAIL,
private_key: process.env.NEXT_PUBLIC_GOOGLE_PRIVATE_KEY?.replace(
/\\n/g,
'\n'
),
},
scopes: [
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/spreadsheets',
],
})
const sheets = google.sheets({
auth,
version: 'v4',
})
const submittedAt = new Date().toUTCString()
const response = await sheets.spreadsheets.values.append({
spreadsheetId: process.env.NEXT_PUBLIC_GOOGLE_SHEET_ID,
range: 'A1:F1',
valueInputOption: 'USER_ENTERED',
requestBody: {
values: [
[
body.name,
body.company,
body.product,
body.email,
body.phone,
submittedAt,
],
],
},
})
return res.status(201).json({
data: response.data,
})
} catch (error) {
console.log(
'email process with error ',
process.env.NEXT_PUBLIC_GOOGLE_CLIENT_EMAIL
)
// the log fo r this error is down below
console.log('error.code', error)
return res.status(error.code).send({ message: error.message })
}
}
error.code 错误:传入的 JSON 对象不包含 client_email 字段 error.code 错误:传入的 JSON 对象不包含 client_email 字段
Ps,秘密是通过aws注入的,并且在云监视日志中可见。
问题1,我的 Dockerfile 中是否需要包含这些机密?
Question2,是否与CSP相关? (尚未实施)
** 更新
我尝试在 dockerfile 中设置密钥但不起作用
还尝试将带有类组件的 csp 添加到 _document.js/ 或添加下一个配置,但效果不佳
** 更新 在生产/开发环境中,尽管它是从保险库注入的,但我无法读取这些值
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我发现
NEXT_PUBLIC_不应该在这里使用,删除它们后它开始工作。但另一个应该使用前缀的秘密(GTM)没有加载更新及解决方案
事实证明,我需要调用
server.js并请求env变量:server.js是这样的:export default function handler(req, res) { const publicEnv = Object.keys(process.env) .filter((key) => key.startsWith('NEXT_PUBLIC')) .reduce((acc, key) => { acc[key] = process.env[key] return acc }, {}) res.status(200).json(publicEnv) }