使用XOAUTH通过IMAP访问Gmail收件箱

时间:2010-09-20 21:45:58

标签: python oauth gmail imap

我正在关注example,我已经授权我的应用程序(=获取访问令牌),但尝试访问收件箱结果:

  21:14.12 > NDIB1 AUTHENTICATE XOAUTH
  21:14.17 < + 
  21:14.17 write literal size 480
  21:14.74 < NDIB1 NO [ALERT] Invalid credentials (Failure)
  21:14.74 NO response: [ALERT] Invalid credentials (Failure)

这是代码:

import xoauth
import oauth2 as oauth
import imaplib

consumer = oauth.Consumer('anonymous', 'anonymous')
access_token = oauth.Token(my_access_token, my_access_token_secret)
user = my_gmail_address
proto = "imap" # is it right???
xoauth_requestor_id = user # and this???
xoauth_string = xoauth.GenerateXOauthString(consumer, access_token, user, proto, xoauth_requestor_id, '', '')
conn = imaplib.IMAP4_SSL('imap.googlemail.com')
conn.debug = 4
conn.authenticate('XOAUTH', lambda x: xoauth_string)
conn.select('INBOX')
print conn.list()

conn.authenticate()调用失败。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

上面的示例使用“匿名”进行身份验证,但您必须将“使用者密钥”和“消费者密钥”设置为您已在“管理域”控制面板中设置的值:

https://accounts.google.com/ManageDomains

示例:

consumer = oauth.Consumer(<OAuth-Consumer-Key>, <OAuth-Consumer-Secret>)