我正在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
密钥来自env,如下所示
export GOOGLE_SERVICE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----**************\n-----END PRIVATE KEY-----\n"
发布于 2018-12-13 22:56:52
我也有同样的问题,经过几个小时的搜索,我发现下面的修复方法对我有效。当把钥匙带过来时,添加了额外的斜杠。
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'
]
)https://stackoverflow.com/questions/51020113
复制相似问题