Chapter 13 — Building an API from the HTML-Only Version 195
Dealing with Threads
Here’s the problem, however: Gmail’s individual message page doesn’t show an
individual message. Rather, it shows parts of an entire thread, and the entire mes-
sage of the last one in the thread.
However, look at the top right of the individual message page. There’s a link to
“Expand All.” Clicking this link brings you a page that shows all of the content
of all of the messages within that particular ThreadID. To test this, I sent a series
of messages to my Gmail account with the same subject line. Gmail naturally
compiled these messages into a thread. The URL for the default view (the
one displaying the latest message in full, but the previous messages’ headers
only) washttp://gmail.google.com/gmail/h/o1xhaxisf335/
?th=102f31cbbb3d650f&v=c.
The Expand All view’s URL was
http://gmail.google.com/gmail/h/60blkjl9nnjc/
?d=e&th=102f31cbbb3d650f&v=c.
The addition of the single flag d=ecauses Gmail to return all of the information
you need. You already know that the random string in the middle of the URL is a
cache-busting string and can be anything, so you can say that the URL to retrieve
a full message thread is http://gmail.google.com/gmail/h/RANDOMSTRING/
?d=e&th=THREADID&v=c.
One thing remains to check. What happens if you try this URL with a ThreadID
of a thread with only one message? Will it still work? The answer, which you can
test yourself, is yes. It does. So now you can see how to read the mail in the Inbox.
You just need to make two passes with your scraping code. The first runs through
the Inbox listing, grabbing the ThreadIDs of each message. The second pass takes
that ThreadID and makes it into a URL as described. You then need only to
retrieve that page and scrape it to read the messages.
Dealing with Other Folders
As you may be noticing, working with the HTML-only version of Gmail is much
easier than the JavaScript version — when it comes to making an API, at least. It’s
a very steady, almost plodding, discovery of techniques. The next thing to look for,
I think, is how to read messages in the other folders: Starred, Sent Mail, All Mail,
Drafts, and so on.