栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Node.js中的服务帐户域范围内的委托通过Google Apps Gmail发送邮件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Node.js中的服务帐户域范围内的委托通过Google Apps Gmail发送邮件

因此,我离解决方案只差半步了,问题是在创建时

const jwtClient = newgoogle.auth.JWT(googleKey.client_email, null, googleKey.private_key,['https://www.googleapis.com/auth/gmail.send'], null);
我没有提到要模拟的帐户。

正确的初始化应为:

const jwtClient = new google.auth.JWT(googleKey.client_email, null,googleKey.private_key, ['https://www.googleapis.com/auth/gmail.send'],'user@domain.com');

总而言之,正确的步骤是:

  1. 在Google Cloud Platform中创建了一个项目
  2. 创建一个服务帐号
  3. Domain Wide Delegation
    为服务帐户启用
  4. 以JSON格式下载了服务帐户的密钥
  5. API Manager
    >
    Credentials
    我创建了
    OAuth 2.0 Client ID
  6. 为项目启用了Gmail API

在Google Apps管理控制台中:

  1. Security
    >
    Advanced Settings
    >
    Manage API client access
    我已经添加了
    Client ID
    从上述步骤4
  2. 我添加了所有可能的范围
    Client ID

这是发送邮件的代码:

const google = require('googleapis');const googleKey = require('./google-services.json');const jwtClient = new google.auth.JWT(googleKey.client_email, null, googleKey.private_key, ['https://www.googleapis.com/auth/gmail.send'], '<user to impersonate>');jwtClient.authorize((err, tokens) => {  if (err) {    console.err(err);    return;  }  console.log('Google auth success')  var gmail = google.gmail({version: 'v1'})  var raw = <build base64 string according to RFC 2822 specification>  var sendMessage = gmail.users.messages.send({    auth: jwtClient,    userId: '<user to impersonate>',    resource: {      raw: raw    }  }, (err, res) => {     if (err) {       console.error(err);     } else {       console.log(res);     } });

希望对其他人有帮助



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/406592.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号