Gmail new mail checker
Posted: Fri Mar 26, 2010 3:57 pm
Hey all, I recently found a really cool chrome extension that sits on top of chrome and checks for new mail. I thought this was really cool so I thought I'd try to see how it works & see if I could do something similar in PB.
It appears that google doesn't really have any api that lets you directly access your mailbox. However, they do have an atom feed that has all of your new mail in it (https://mail.google.com/mail/feed/atom). So, this program uses google api to grab an authentication token and I kinda hacked it up to pass that to the atom feed to get the mail. I couldn't find any documentation on how to use the auth token for logging into the atom feed, so it was a lot of reading through headers to see what was going on. Many thanks to the people who made live headers for firefox
.
So, here's my proof of concept, quick and dirty like.
Maybe nothing special but I'm quite proud
.
It appears that google doesn't really have any api that lets you directly access your mailbox. However, they do have an atom feed that has all of your new mail in it (https://mail.google.com/mail/feed/atom). So, this program uses google api to grab an authentication token and I kinda hacked it up to pass that to the atom feed to get the mail. I couldn't find any documentation on how to use the auth token for logging into the atom feed, so it was a lot of reading through headers to see what was going on. Many thanks to the people who made live headers for firefox

So, here's my proof of concept, quick and dirty like.
Code: Select all
;epidemicz 3/26/2010
;quick and dirty (really dirty) proof of concept gmail new mail checker
;be sure to fill out user & pass
user$ = "user"
pass$ = "pass"
email$ = user$ + "%40gmail%2Ecom"
;needed for content-length: header -- google api doc doesn't say you need this
;but if you don't send content-length header with the request it will give error
;len is basically the length of the last section of the first request$
len = 57 + Len(email$) + Len(pass$)
InitNetwork()
*Buffer = AllocateMemory(3000)
connid = OpenNetworkConnection("mail.google.com", 80)
;request to get a ClientLogin authentication token from google
;http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html
request$ = "POST https://www.google.com/accounts/ClientLogin HTTP/1.0" + Chr(10)
request$ + "Content-type: application/x-www-form-urlencoded" + Chr(10)
request$ + "Connection: keep-alive" + Chr(10)
request$ + "Content-length: " + Str(Len) + Chr(10) + Chr(10)
request$ + "&Email=" + email$ + "&Passwd=" + pass$ + "&accountType=HOSTED_OR_GOOGLE&service=mail"
SendNetworkString(connid, request$)
Debug "Logging in.."
Delay(2000)
Repeat
cEvent = NetworkClientEvent(connid)
Select cEvent
Case #PB_NetworkEvent_Data
ReceiveNetworkData(connid, *Buffer, 3000)
response$ = PeekS(*Buffer)
FreeMemory(*Buffer)
*Buffer = AllocateMemory(3000)
For x = 1 To CountString(response$, Chr(10))
curline$ = StringField(response$, x, Chr(10))
Debug curline$
If FindString(curline$, "Auth=", 0)
;for some reason the auth token must have GX= in front of it
auth$ = "GX=" + Mid(curline$, 6)
Debug "sending request for mail feed.."
;forming request for gmail atom feed
request$ = "GET /mail/feed/atom/ HTTP/1.1" + Chr(10)
request$ + "Host: mail.google.com" + Chr(10)
request$ + "Cookie: " + auth$ + Chr(10) + Chr(10)
SendNetworkString(connid, request$)
Delay(2000)
EndIf
Next x
EndSelect
Until cEvent = 0
