oauth2和imap与Gmail的连接

时间:2013-07-31 17:08:14

标签: oauth-2.0 imap google-oauth gmail-imap imaplib

我需要使用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()

但我在这里无法理解ConsumerToken

  1. 他们是什么意思?
  2. 我如何分别为他们获取密钥和秘密?
  3. 我从https://code.google.com/p/google-mail-oauth2-tools/wiki/OAuth2DotPyRunThrough获得的client_id和client_secret。这是Consumer还是Token

1 个答案:

答案 0 :(得分:2)

上面的代码示例适用于OAuth 1,而不是OAuth 2.使用者密钥和密钥,令牌和令牌密钥都是OAuth 1条款。

我认为混淆是由于使用的Python库名为“oauth2”。据我所知,这是OAuth 1库的第二个版本,这个名字很不幸。

OAuth 2与Gmail一起使用的文档位于: https://developers.google.com/gmail/oauth_overview