Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何使用Gmail API连接nodejs服务器中的G-Suite_Node.js_Google Api_Gmail Api_Service Accounts_Google Api Nodejs Client - Fatal编程技术网

Node.js 如何使用Gmail API连接nodejs服务器中的G-Suite

Node.js 如何使用Gmail API连接nodejs服务器中的G-Suite,node.js,google-api,gmail-api,service-accounts,google-api-nodejs-client,Node.js,Google Api,Gmail Api,Service Accounts,Google Api Nodejs Client,我想使用Gmail API在nodejs服务器上访问我的G-Suite帐户。 我知道我应该创建服务帐户并使用其凭据进行身份验证。 我尝试了很多例子和方法,但都没有成功 这是我最后一次尝试了。 返回400个错误请求 代码:400, 错误:[ { 域:“全局”, 原因:“失败的前提条件”, 消息:“请求错误” } ] const{GoogleAuth}=require('google-auth-library'); const-credentials=require('./sevice-accou

我想使用Gmail API在nodejs服务器上访问我的G-Suite帐户。 我知道我应该创建服务帐户并使用其凭据进行身份验证。 我尝试了很多例子和方法,但都没有成功

这是我最后一次尝试了。 返回400个错误请求

代码:400, 错误:[ { 域:“全局”, 原因:“失败的前提条件”, 消息:“请求错误” } ]

const{GoogleAuth}=require('google-auth-library');
const-credentials=require('./sevice-account-credentials.json');
异步函数main(){
const clientEmail=credentials.client\u电子邮件;
const privateKey=credentials.private_key;
如果(!clientEmail | |!privateKey){
抛出新错误(`
客户端电子邮件和私钥环境变量是
这个样本。
`);
}
const auth=new GoogleAuth({
证书:{
客户电子邮件:clientEmail,
私钥:私钥,
},
范围:'https://mail.google.com/',
});
const client=wait auth.getClient();
const projectId=wait auth.getProjectId();
常量url=`https://www.googleapis.com/gmail/v1/users/my-gsuite@domain.co.il/labels/label_id`;
const res=wait client.request({url});
console.log(res.data);
}
main().catch(console.error)问题:
您没有模拟域中的任何帐户。这就是域范围授权的要点:模拟/代表另一个帐户行事

解决方案: 在实例化
GoogleAuth
时,您必须通过提供属性
clientOptions
来指定希望服务帐户代表哪个帐户:

clientOptions:{subject:“我的-gsuite@domain.co.il" }
因此,这就像:

const auth=new GoogleAuth({
证书:{
客户电子邮件:clientEmail,
私钥:私钥,
},
范围:'https://mail.google.com/',
客户选项:{主题:“我的-gsuite@domain.co.il" }
});
参考:

请编辑您的问题并包含完整的错误消息。在你做任何事情之前,你需要先准备好谢谢。我已经添加了错误。域范围的委派设置为true。只是为了说明:域范围的委派为true,并且仍然抛出此错误。您好,我发布了一个关于此问题的答案。你能澄清一下这是否解决了你的问题吗?解决了!多谢各位@马布鲁克,不客气!很高兴你的问题解决了。