尝试在Node.js上使用服务帐户连接到Gmail API。

huangapple go评论69阅读模式
英文:

trying to connect to gmail api using a service account on nodejs

问题

这是我第一次使用Gmail API。我试图通过服务账号使用Gmail API。我的程序目标是仅解析我的电子邮件并查找特定关键词,然后与本地数据库进行比较。然而,我无法连接到Gmail API。我已经完成了创建服务账号、下载服务账号密钥JSON文件以及授予服务账号域代理权限的步骤。我还已经给予了https://mail.google.com/的范围。我只是试图证明我可以连接并且身份验证通过,通过进行一个getprofile调用。

这段代码不起作用,因为我返回了一个前提条件检查失败的错误。

唯一的代码我得到了一个400的错误,错误消息是前提条件检查失败。

我希望能看到get profile Gmail API调用的输出。我已经阅读了一些资料,我唯一的猜测是我需要做一些关于JWT的事情,这让我感到困惑。JWT和您下载的服务账号JSON文件是相同的吗?我感激任何人能提供的见解,谢谢。

英文:

It is my first time using the gmail api. I am trying to use the gmail api via a service account. The goal of my program is to parse emails through my email only and to look for certain keywords and compare them to a local database. However I am unable to connect to the gmail api. I did do the steps of creating the service account, downloading the service account key json file and giving permission to domain wide delegation to the service account. I have also given the scope of https://mail.google.com/. I am simply trying to prove I can connect and the authentication works by doing a getprofile call.

const {google} = require('googleapis');

const auth = new google.auth.GoogleAuth({
  keyFile: './serviceaccountkey.json',
  scopes: ['https://mail.google.com/'],
  subject: 'admin@example.org',
});


const gmail1 = google.gmail({version: 'v1', auth: auth});


async function temp1(){

  var cont = gmail1.users.getProfile({userId: "me",}) 
  return cont;
}

console.log(temp1());

This does not work as I return an error of precondition check failed.

           throw new common_1.GaxiosError(`Request failed with status code ${translatedResponse.status}`, opts, translatedResponse);
                      ^

GaxiosError: Precondition check failed.
    at Gaxios._request (C:\Users\user\Documents\FunctionAzure\node_modules\gaxios\build\src\gaxios.js:130:23)

the only code I get is 400 with message of precondition check failed.

I was expecting to see output for the get profile gmail api call. I have done some reading and my only guess is I need to do something with JWTs which I am confused about. Are JWTs and service account json file you download the same thing? I appreciate any insight anyone can offer, thank you

答案1

得分: 1

根据您的身份验证方式,我认为您应该直接请求公开 'auth' 的 gmail 子模块,即您的代码应该如下所示:

// 请求公开 auth 的 gmail 模块
const gmail = require('@googleapis/gmail') 

// 提供身份验证信息
const auth = new gmail.auth.GoogleAuth({
  keyFile: './serviceaccountkey.json',
  scopes: ['https://mail.google.com/'],
  subject: 'admin@example.org',
});

// 创建身份验证客户端
const authClient = await auth.getClient();

// 对 Gmail 进行身份验证并创建 Gmail 客户端
const gmail1 = await gmail.gmail({
    version: 'v1',
    auth: authClient
});

查看其文档中的示例 此处(查找 - 或者,您可以通过安装子模块直接调用 API 部分)。

英文:

Based on how you're authenticating, I think you should directly request the gmail submodule which exposes 'auth' i.e. your code should be

// request the gmail module which exposes auth
const gmail = require('@googleapis/gmail') 

// provide information for authenticating
const auth = new gmail.auth.GoogleAuth({
  keyFile: './serviceaccountkey.json',
  scopes: ['https://mail.google.com/'],
  subject: 'admin@example.org',
});

// create an auth client
const authClient = await auth.getClient();

// authenticate to gmail and create a gmail client
const gmail1 = await gmail.gmail({
    version: 'v1',
    auth: authClient
});

See the example in their documentation here (look for the section - Alternatively, you can make calls directly to the APIs by installing a submodule)

huangapple
  • 本文由 发表于 2023年6月29日 03:52:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定