0

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

但我无法理解Consumerand Token

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

1 回答 1

2

上面的代码示例适用于 OAuth 1,而不是 OAuth 2。消费者密钥和秘密、令牌和令牌秘密都是 OAuth 1 术语。

我认为混淆是由于使用的 Python 库被称为“oauth2”这一事实造成的。据我了解,这是 OAuth 1 库的第二个化身,这个名字很不幸。

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

于 2013-08-01T17:23:02.870 回答