我想编写一个应用程序来处理我的一些以某种方式标记的 Gmail 电子邮件。
示例代码here https://github.com/googleapis/google-api-nodejs-client/blob/master/samples/gmail/list.js给了我代码的起点(我使用 Promise 而不是 async wait 重写了代码):
'use strict';
const path = require('path');
const { google } = require('googleapis');
const { authenticate } = require('@google-cloud/local-auth');
authenticate({
keyfilePath: path.join(__dirname, 'key.json'),
scopes: [
'https://www.googleapis.com/auth/gmail.readonly',
],
}).then(auth => {
google.options({ auth })
gmail.users.messages.list({
userId: "me",
}).then((res) => {
console.log(res.data)
}).catch(err => {
console.log(err)
})
}).catch(err => {
console.log(err);
})
以下是我到目前为止所采取的步骤:
- 创建了一个谷歌云项目
- 创建具有所有者角色的服务帐户
- 从服务帐户下载密钥文件并将其复制到我的代码目录中,如下所示
key.json
- 运行代码:
GOOGLE_APPLICATION_CREDENTIALS="$(pwd)/key.json" node index.js
这是我收到的错误:
TypeError: Cannot read property 'redirect_uris' of undefined
at authenticate (/home/user/dev/gmail-processor/node_modules/@google-cloud/local-auth/build/src/index.js:46:15)
at Object.<anonymous> (/home/user/dev/gmail-processor/index.js:7:1)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
看起来它需要带有重定向 url 的 oauth 凭据。另外,我导出似乎是多余的GOOGLE_APPLICATION_CREDENTIALS
当我包括keyfilePath
当我打电话时authenticate
。我做错了什么,如何让这段代码成功执行?
我通过使用以下方法解决了这个问题:
const auth = new google.auth.GoogleAuth({
keyFile: path.join(__dirname, 'key.json'),
scopes: ['https://www.googleapis.com/auth/gmail.readonly'],
});
代替:
const auth = await authenticate({
keyfilePath: path.join(__dirname, 'key.json'),
scopes: ['https://www.googleapis.com/auth/gmail.readonly'],
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)