javascript - 谷歌云功能,如何设置keyFile路径避免报错?
问题描述
我创建了一个使用 Gmail API 向我发送电子邮件的云功能。云功能由谷歌云调度器触发。当我在本地测试时,它似乎可以工作,但是当我将我的函数上传到谷歌云函数代码编辑器并上传时,日志中有错误。请注意,该功能似乎部署良好并且处于活动状态。
这是日志中的错误:
我不确定它指的是什么 keyFile 或如何设置它。这是我的代码:
/**
* Triggered from a message on a Cloud Pub/Sub topic.
*
* @param {!Object} event Event payload.
* @param {!Object} context Metadata for the event.
*/
const {google} = require('googleapis');
const {authenticate} = require('@google-cloud/local-auth');
const gmail = google.gmail('v1');
const sendMail = async () => {
// Obtain user credentials to use for the request
const auth = await authenticate(
process.env.client_id,
process.env.client_secret,
[
'https://mail.google.com/',
'https://www.googleapis.com/auth/gmail.compose',
'https://www.googleapis.com/auth/gmail.send',
],
);
google.options({auth});
// You can use UTF-8 encoding for the subject using the method below.
// You can also just use a plain string if you don't need anything fancy.
const subject = ' Hello ';
const utf8Subject = `=?utf-8?B?${Buffer.from(subject).toString('base64')}?=`;
const messageParts = [
'From: Oamar Kanji <oamarkanji@gmail.com',
'To: Oamar Kanji <oamarkanji@gmail.com>',
'Content-Type: text/html; charset=utf-8',
'MIME-Version: 1.0',
`Subject: ${utf8Subject}`,
'',
'This is a message just to say hello.',
'So... <b>Hello!</b> ❤️',
];
const message = messageParts.join('\n');
// The body needs to be base64url encoded.
const encodedMessage = Buffer.from(message)
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
const res = await gmail.users.messages.send({
userId: 'me',
requestBody: {
raw: encodedMessage,
},
});
console.log(res.data);
return res.data;
}
exports.alertPubSub = async (event, context) => {
await sendMail()
};
解决方案
推荐阅读
- email - 使用 curl 请求发送动态模板时 dynamic_template_data 不起作用 - Sendgrid API
- c# - 为什么返回数组在c#的函数中不起作用
- r - ld:未知选项:-platform_version 从源代码构建 R 包时
- javascript - 提交时删除输入字段验证消息
- python - 使用 Python 在 Selenium 中执行 javascript
- javascript - 调整图像大小并转换为base64或转换为base64然后调整大小更有效的是什么?(node.js - 没有画布)
- sdk - 从 Google Assistant SDK 激活 GPIO 或任何 Python 脚本
- groovy - Groovy:如何通过属性将地图列表合并到单个地图,将不同的地图值组合到内部列表中
- spss - 比较所有可能的连续变量对
- jquery - 通过卡片内的按钮制作 3d 变换卡片,而不是通过单击整个卡片