A simple tool that gets emails from the Gmail API
npm install gmail-getterContributions are very welcome!
Steps to go:
1. Create a project in Google Cloud Console.
2. Create OAuth credentials in API & Services section (_preferably, select Desktop app there if you need it for automated tests_) and then download it.
3. Enable Gmail API.
4. Obtain a Refresh token.
To get a Refresh token - simply execute a command in a project root:
> npx gmail-getter get-refresh-token
(or anywhere else if you've got the package installed globally)
> get-refresh-token
You must put credentials.json file (p.2) in a place where you execute the command.
> [!WARNING]
> Credentials file name is name-sensitive - rename it to credentials.json if it's named different.
> [!IMPORTANT]
> Refresh token expires after some time, especially if you don't use it for a while. Keep that in mind, since you'd need to generate a new one from time to time.
``ts
// ES6+
import {getAccessToken, checkInbox} from 'gmail-getter'
// CommonJS
const {getAccessToken, checkInbox} = require('gmail-getter')
`
or the whole package:
`ts
// ES6+
import gmail from 'gmail-getter'
// CommonJS
const gmail = require('gmail-getter')
`
`ts
import {getAccessToken} from 'gmail-getter'
const accessToken = await getAccessToken(
'your-client-id',
'your-client-secret',
'your-refresh-token'
)
`
It'd probably be a good idea to get it on the earlier stages of a test run (global setup?) and store it as an environment variable.
`ts`
const email = await checkInbox({
token: 'your-access-token',
query: 'from:your-email-username subject:Your-Email-Subject'
})
Options available:
* token is an access tokentimeout
* (optional) sets maximum execution time for the functionstep
* (optional) sets timeout between retries to fetch an emailall
* (optional) says whether to find a single email or all matching the query criteriaquery
* (optional) lets you filter out emails you want to find, you can find more about queries therelogs
* (optional) turns on or off polling logs of attempts to get emails
`ts`
const html = parseHtmlFromEmail(email)
* email is an email that you fetch with checkInbox function
If you're maintaining an automated test - this might be the best option, as you can verify contents of the email and click any links inside of it.
`ts
import {getAccessToken, checkInbox} from 'gmail-getter'
const accessToken = await getAccessToken(
'your-client-id',
'your-client-secret',
'your-refresh-token'
)
const email = await checkInbox({
token: accessToken,
query: 'your-query'
})
``