I want to implement custom code to send emails using the Google API. Currently, I have achieved this by adding a refresh token as a static value, but since the refresh token will expire after some time, I need a solution to handle its renewal.
Hi, @KPadhiyar !
Thanks for reaching out to the Community!
I would like to invite some members of our community who may offer valuable insights.— hey @WesQ @nickdeckerdevs1 @JMartínez5, @SteveHTM Could you share your perspective on how you would address this situation?
Thanks for taking a look!
Diana
You will need to set up an authentication server which will handle your google app authentication.
Can I ask what you are attempting to do with this integration? Please explain the use case and purpose behind this. Who are you sending emails to, what is their content, why Gmail? There are so many systems out there like sendgrid and mailjet and others that can accomplish this with much less needed infrastructure.
the way oauth refresh works is you need a place to authorize into and then also refresh your tokens. Because oauth typically requires some redirects amd routing, this is typically done outside of hubspot serverless functions. A small cloud server to manage this is very typical. Depending on complexity of this you could need a db or some caching.
Here is a quick start guide. Again, this may not be the direction you really want to go with this so please provide more details and I can help guide you
Another approach -similar to what @nickdeckerdevs1 suggested- is to offload the email-sending logic to your own backend. I’d recommend setting up a custom backend service to handle both the Google API authentication and the actual email-sending logic. Then, in HubSpot, you can create a Custom Workflow Action (guide here) that makes a POST request to your backend, passing along the necessary fields for the email (like to, subject, message, etc.).
This setup has a few key benefits:
- All sensitive credentials (like your client_secret and refresh_token) stay securely on your server.
- You can implement logic to automatically refresh the access token using the long-lived refresh_token—the Google API client libraries handle this automatically.
- HubSpot simply triggers the request, acting as a secure and scalable way to initiate the email.
By centralizing the logic on your backend, you’re keeping your system secure, modular, and much easier to maintain long-term.
Yup. This is where I was going to point them if they really needed to use google apis to send this mail.
I don’t understand the actual use case for this. If you are building out an app, then you would need to have this backend system setup. If you are building this out to send as one person - why not have it send through HubSpot?
if this is more of a system notification type email then using a service like sendgrid makes more sense.
I was not prepared to write all of what you wrote from my phone before I knew that, but thank you for detailing it nicely!