Send mail via Google Apps Gmail using service account domain wide delegation in nodejs

后端 未结 2 1016
名媛妹妹
名媛妹妹 2020-12-02 02:13

I\'ve been reading tutorials and seeing examples for 2 days already, with no success. I want to send an email using Google Apps Gmail account in NodeJS environment, however,

2条回答
  •  渐次进展
    2020-12-02 02:50

    So I was half-step close to the solution, the problem was that while creating const jwtClient = new google.auth.JWT(googleKey.client_email, null, googleKey.private_key, ['https://www.googleapis.com/auth/gmail.send'], null); i did not mention the account to be impersonated.

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

    To summarize, the correct steps are:

    1. Created a project in Google Cloud Platform
    2. Created a service account
    3. Enabled Domain Wide Delegation for the service account
    4. Downloaded the key for the service account in JSON format
    5. API Manager > Credentials i have created OAuth 2.0 Client ID
    6. Enabled Gmail API for the project

    In Google Apps Admin console:

    1. In Security > Advanced Settings > Manage API client access i have added the Client ID from step 4 above
    2. I have added all possible scopes for the Client ID

    This is the code that sends mails:

    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'], '');
    
    jwtClient.authorize((err, tokens) => {
      if (err) {
        console.err(err);
        return;
      }
      console.log('Google auth success')
      var gmail = google.gmail({version: 'v1'})
      var raw = 
    
      var sendMessage = gmail.users.messages.send({
        auth: jwtClient,
        userId: '',
        resource: {
          raw: raw
        }
      }, (err, res) => {
         if (err) {
           console.error(err);
         } else {
           console.log(res);
         }
     });
    

    Hope that would be helpful for others

提交回复
热议问题