我需要使用oauth2和imap连接Gmail,我可以看到来自https://github.com/simplegeo/python-oauth2的代码:
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()
但我在这里无法理解Consumer
和Token
。
Consumer
还是Token
?答案 0 :(得分:2)
上面的代码示例适用于OAuth 1,而不是OAuth 2.使用者密钥和密钥,令牌和令牌密钥都是OAuth 1条款。
我认为混淆是由于使用的Python库名为“oauth2”。据我所知,这是OAuth 1库的第二个版本,这个名字很不幸。
OAuth 2与Gmail一起使用的文档位于: https://developers.google.com/gmail/oauth_overview