javascript - TypeError:无法读取未定义的属性“redirect_uris”
问题描述
我想编写一个应用程序来处理我的一些以某种方式标记的 gmail 电子邮件。
这里的示例代码为我的代码提供了一个起点(我已经使用 promises 而不是 async await 重写了它):
'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'],
});
推荐阅读
- r - Is there an R function to convert columns to given types?
- c++ - AVX2 列填充计数算法分别在每个位列上
- ios - AVAudioEngine.connect 在硬件而非模拟器上崩溃
- kotlin - Kotlin:不能在外部枚举类中使用来自伴随对象的 const val
- ssh - Is is possible to use the Web Authenitcation API with SSH keys?
- arrays - 用于沿大矩阵的对角线在 3D 数组中插入 nxn 矩阵的代码向量化
- flutter - Flutter - 如何制作一个自定义小部件,为接收到的可滚动小部件添加页脚?
- java - 关系运算中的操作数类型转换
- elasticsearch - ElasticSearch 无痛脚本中的 score_ 始终返回 1
- python - 如何在没有写入所有目录的情况下在 python 中压缩文件