Okay, so I haven’t been able to get the Service Account thing to work, but I ended up installing the last version of googleapis (npm i -S googleapis@32.0.0). The issues I had to fix were the following:
- The
tokensobject returned by.refreshAccessToken()doesn’t have a property calledaccess_token. Instead, theaccess_tokenis found inside another object calledcredentials. (I’m usingasync/await)
const tokens = await oauth2Client.refreshAccessToken()
const accessToken = tokens.credentials.access_token2. If you don’t use async/await, I think you may need to put the smtpTransporter initialization code (nodemailer.createTransport) INSIDE the callback for the .refreshAccessToken() method. I could be mistaken here though, but I think I was running into a problem where it was taking longer to get the updated access_token than it was to create the transporter which required the access_token to work.
3. This one isn’t a problem really, but when you’re using nodemailer.createTransport(), you can trade out the host, port, and secure properties for just service: “gmail” and it will add those things for you under the hood. See here for the full list of well-known services.
I’m too sick of trying to get this to work with the Service Account that I’m going to stick with this solution. Thanks again for your great article!