Bob Ziroll
Aug 31, 2018 · 1 min read

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:

  1. The tokens object returned by .refreshAccessToken() doesn’t have a property called access_token. Instead, the access_token is found inside another object called credentials. (I’m using async/await)
const tokens = await oauth2Client.refreshAccessToken()
const accessToken = tokens.credentials.access_token

2. 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!

    Bob Ziroll

    Written by

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade