使用imaplib和oauth连接Gmail

19 投票
3 回答
19166 浏览
提问于 2025-04-16 13:00

我想在Python中使用Oauth来连接Gmail。目前我已经从Google那里拿到了xoauth.py这个脚本(链接),生成令牌的过程都很顺利,但我接下来该怎么在另一个脚本中使用这个令牌呢?这个脚本会在Django中运行。

现在我的脚本是这样登录的:

m = imaplib.IMAP4_SSL("imap.gmail.com")
m.login("example@gmail.com", "password")

但我想要一个更安全的方式。

3 个回答

3

这里有一个例子,教你如何使用谷歌的 xoauth.py 文件来连接到 IMAP。这个例子会输出一些调试信息,所以如果你要做真正的应用,可能最好使用 oauth 这个包。至少这个例子可以帮助你入门:

import imaplib
import random
import time

import xoauth

MY_EMAIL = 'xxx@gmail.com'
MY_TOKEN = # your token
MY_SECRET = # your secret

def connect():
    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_conn = imaplib.IMAP4_SSL('imap.googlemail.com')
    imap_conn.authenticate('XOAUTH', lambda x: token)
    imap_conn.select('INBOX')

    return imap_conn

connect()
8

谷歌有一个很好的示例代码,可以用来做OAuth2和IMAP。另外,确保你的权限范围设置正确。

'scope': 'https://mail.google.com/'
'access_type': 'offline'

下面是谷歌示例中的代码片段

import base64
import imaplib

my_email = "xyz@gmail.com"
access_token = ""    #Oauth2 access token

auth_string = GenerateOAuth2String(my_email, access_token, base64_encode=False)
TestImapAuthentication(my_email, auth_string)


def TestImapAuthentication(user, auth_string):
  """Authenticates to IMAP with the given auth_string.

  Prints a debug trace of the attempted IMAP connection.

  Args:
    user: The Gmail username (full email address)
    auth_string: A valid OAuth2 string, as returned by GenerateOAuth2String.
        Must not be base64-encoded, since imaplib does its own base64-encoding.
  """
  print
  imap_conn = imaplib.IMAP4_SSL('imap.gmail.com')
  imap_conn.debug = 4
  imap_conn.authenticate('XOAUTH2', lambda x: auth_string)
  imap_conn.select('INBOX')


def GenerateOAuth2String(username, access_token, base64_encode=True):
  """Generates an IMAP OAuth2 authentication string.

  See https://developers.google.com/google-apps/gmail/oauth2_overview

  Args:
    username: the username (email address) of the account to authenticate
    access_token: An OAuth2 access token.
    base64_encode: Whether to base64-encode the output.

  Returns:
    The SASL argument for the OAuth2 mechanism.
  """
  auth_string = 'user=%s\1auth=Bearer %s\1\1' % (username, access_token)
  if base64_encode:
    auth_string = base64.b64encode(auth_string)
  return auth_string
16

这里有一个例子,使用oauth2模块来进行oauth认证,这段内容摘自说明文档:

import oauth2 as oauth
import oauth2.clients.imap as imaplib

# Set up your Consumer and Token as per usual. Just like any other
# three-legged OAuth request.
consumer = oauth.Consumer('your_consumer_key', 'your_consumer_secret')
token = oauth.Token('your_users_3_legged_token', 
    'your_users_3_legged_token_secret')

# Setup the URL according to Google's XOAUTH implementation. Be sure
# to replace the email here with the appropriate email address that
# you wish to access.
url = "https://mail.google.com/mail/b/your_users_email@gmail.com/imap/"

conn = imaplib.IMAP4_SSL('imap.googlemail.com')
conn.debug = 4 

# This is the only thing in the API for impaplib.IMAP4_SSL that has 
# changed. You now authenticate with the URL, consumer, and token.
conn.authenticate(url, consumer, token)

# Once authenticated everything from the impalib.IMAP4_SSL class will 
# work as per usual without any modification to your code.
conn.select('INBOX')
print conn.list()

比起使用xoauth,这个方法要干净很多。

撰写回答