我正在使用nodejs应用程序中的googleapis,并且尝试与Gmail帐户中的日历进行交互。当我在本地计算机上测试它时,它可以正常工作,但是在部署它时,我得到了错误
5|index | Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
5|index | at Sign.sign (crypto.js:331:26)
5|index | at Object.sign (/home/ec2-user/api/node_modules/jwa/index.js:55:45)
5|index | at Object.jwsSign [as sign] (/home/ec2-user/api/node_modules/jws/lib/sign-stream.js:23:24)
5|index | at GoogleToken.<anonymous> (/home/ec2-user/api/node_modules/gtoken/src/index.ts:251:13)
5|index | at step (/home/ec2-user/api/node_modules/gtoken/build/src/index.js:42:23)
5|index | at Object.next (/home/ec2-user/api/node_modules/gtoken/build/src/index.js:23:53)
5|index | at /home/ec2-user/api/node_modules/gtoken/build/src/index.js:17:71
5|index | at new Promise (<anonymous>)
下面是我尝试使用它的控制器。
import { google } from 'googleapis'
import { Request, Response, NextFunction } from 'express';
export class HolidayController {
fetchHolidays(req: Request, res: Response, next: NextFunction) {
const jwtClient = new google.auth.JWT(
process.env.GOOGLE_SERVICE_CLIENT_EMAIL,
null,
process.env.GOOGLE_SERVICE_PRIVATE_KEY,
[
'https://www.googleapis.com/auth/calendar'
]
)
const calendar = google.calendar({ version: 'v3', auth: jwtClient});
calendar.events.list({
calendarId: 'en.ae#holiday@group.v.calendar.google.com',
timeMin: (new Date()).toISOString(),
orderBy: 'startTime',
singleEvents: true,
}, (err, response: any) => {
if (err) {
return next(err)
}
if (response.data.items.length == 0) {
return res.status(200).json({ message: 'No events in calendar' })
}
res.status(200).json({ events: response.data.items })
})
}
}
最近3个小时都在此使用。有帮助吗?
错误来自调用calendar.events.list
密钥来自这样的环境
export GOOGLE_SERVICE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----**************\n-----END PRIVATE KEY-----\n"
答案 0 :(得分:0)
我遇到了同样的问题,经过数小时的搜索,我发现以下修复程序对我有用。移开密钥时添加了额外的斜杠。
const fixedKey = process.env.GOOGLE_SERVICE_PRIVATE_KEY.replace(new RegExp("\\\\n", "\g"), "\n")
const jwtClient = new google.auth.JWT(
process.env.GOOGLE_SERVICE_CLIENT_EMAIL,
null,
fixedKey,
[
'https://www.googleapis.com/auth/calendar'
]
)