Here's a Python script to access your Gmail account. First you need to create an OAuth token. Download google xoauth.py and run it. He will lead you up the steps. You will receive a URL to receive a verification code - paste it into the script and it will spit out your token and secret:
% python xoauth.py --generate_oauth_token --user=youremail@gmail.com
, Python script . xoauth.py IMAP, IMAP, "".
import email.message
import imaplib
import random
import time
import xoauth
MY_EMAIL = 'youremail@gmail.com'
MY_TOKEN = '<token>'
MY_SECRET = '<secret>'
nonce = str(random.randrange(2**64 - 1))
timestamp = str(int(time.time()))
consumer = xoauth.OAuthEntity('anonymous', 'anonymous')
access = xoauth.OAuthEntity(MY_TOKEN, MY_SECRET)
token = xoauth.GenerateXOauthString(
consumer, access, MY_EMAIL, 'imap', MY_EMAIL, nonce, timestamp)
imap = imaplib.IMAP4_SSL('imap.googlemail.com')
imap.debug = 4
imap.authenticate('XOAUTH', lambda x: token)
msg = email.message.Message()
msg['Subject'] = 'subject of the message'
msg['From'] = MY_EMAIL
msg['To'] = MY_EMAIL
msg.set_payload('Body of the message')
now = imaplib.Time2Internaldate(time.time())
imap.append('[Gmail]/Drafts', '', now, str(msg))
imap.logout()