Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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 NodeJS:无法使用服务帐户访问Gmail API_Node.js_Gmail Api_Service Accounts - Fatal编程技术网

Node.js NodeJS:无法使用服务帐户访问Gmail API

Node.js NodeJS:无法使用服务帐户访问Gmail API,node.js,gmail-api,service-accounts,Node.js,Gmail Api,Service Accounts,我拥有一个G套件域,并通过域范围的授权开立了一个服务帐户。服务帐户在项目中也具有所有者身份。 我启用了gmail API并添加了引用范围(“将整个域的权限委托给您的服务帐户”) 我也启用了不太安全的应用程序设置 这是我的密码: var {google} = require('googleapis'); var jwtClient = new google.auth.JWT( "service@xxx.iam.gserviceaccount.com", null, "--

我拥有一个G套件域,并通过域范围的授权开立了一个服务帐户。服务帐户在项目中也具有所有者身份。 我启用了gmail API并添加了引用范围(“将整个域的权限委托给您的服务帐户”) 我也启用了不太安全的应用程序设置

这是我的密码:

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

var jwtClient = new google.auth.JWT(
    "service@xxx.iam.gserviceaccount.com",
    null,
    "-----BEGIN PRIVATE KEY-----\n....",
    ['https://mail.google.com/', 
    'https://www.googleapis.com/auth/gmail.readonly', 
    'https://www.googleapis.com/auth/gmail.modify', 
    'https://www.googleapis.com/auth/gmail.metadata'] // I have also tried https://www.googleapis.com/auth/gmail.imap_admin
);

jwtClient.authorize(function(err, tokens) {
    if (err) {
      console.error(err);
      return;
    }
    console.log(tokens); // successful print the token
});
但当我使用此令牌尝试列出电子邮件时: 获取{access_token}

发生错误

{
    "error":{
        "errors":[
            {
                "domain": "global",
                "reason": "failedPrecondition",
                "message": "Bad Request"
            }
        ],
        "code": 400,
        "message": "Bad Request"
    }
}
我不想要解决方法,我打算用服务帐户解决问题。我成功地将Gmail API与其他身份验证选项结合使用。 我读过很多文章,但没有一篇有用。我已经坚持了一个星期,任何建议都将非常感谢


我将随时提供更多细节,以备不时之需。

添加子索赔应能解决此问题

var jwtClient = new google.auth.JWT(
    "service@xxx.iam.gserviceaccount.com",
    null,
    "-----BEGIN PRIVATE KEY-----\n....",
    ['https://mail.google.com/', 
    'https://www.googleapis.com/auth/gmail.readonly', 
    'https://www.googleapis.com/auth/gmail.modify', 
    'https://www.googleapis.com/auth/gmail.metadata'], // I have also tried https://www.googleapis.com/auth/gmail.imap_admin
    'user@yourdomain.com' // use a user email in your domain since service account do not have Gmail box
);

我试过了。。。但是它失败了。。。蚂蚁额外的把戏?(我的服务帐户拥有整个域的授权,由这些服务处理)参数完全相同。。。(属于我的域中的用户帐户除外)并且我得到:错误:unauthorized_client:客户端无权使用此方法检索访问令牌。。。t征求反馈意见: