Now that we have enabled the API and downloaded the credentials json file. We can follow the example on the quickstart instruction to authenticate to gmail API. However, the getNewToken will simply display a URL on the console and you are supposed to manually go to the URL on a browser and copy the code back to the program. But we're writing an automated test with protractor, let's automate that too!
It's mostly the same as the example, except that when token.json is not found, it'll open a new browser window and grab the code automatically. Also, note that the code uses an "AppPo" class, it's just a simple utility class I use to check if the button exists before clicking it.
The gmail API's list method returns a list of emails, only with the message id and the thread id, we'll have to call the get method to retrieve the content. The structure it returned is too complicated to my taste, and after all, I just want to grab the HTML content, so I decided to just get the raw content and reconstruct the HTML. Now you have it, we can now call searchEmail and pass it's returns to getHtmlFromEmailBody to get the HTML content for all emails returned. With cheerio, we can easily find all links like this.
const $ = gmailUtils.getHtmlFromEmailBody(...);
$('a').each( (i,a) => console.log( $(a).attr('href') ) );