【问题标题】:oauth2 and imap connection with Gmailoauth2 和 imap 与 Gmail 的连接
【发布时间】:2013-08-01 08:20:00
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

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

    在 Gmail 中使用 OAuth 2 的文档位于: https://developers.google.com/gmail/oauth_overview

    【讨论】:

    • 感谢您的回答。但是您知道如何使用 OAuth2 实现身份验证吗?任何代码或文档表示赞赏。
    • 我刚刚编辑了答案并提供了文档链接。
    • 我还是一头雾水。说,如果我要访问用户A的gmail,access_token是A生成的,不是我,对吧?
    • 通常您无法访问其他用户的电子邮件。是的,要访问 A 的电子邮件,A 必须批准授权。除非您正在管理托管域,否则您可以使用服务帐户。域管理员必须为您的应用授予模拟访问权限。
    • service account 是什么意思?但我认为oauth2 可以帮助我们访问其他用户的电子邮件,而无需知道他们的密码,对吧?
    猜你喜欢
    • 2014-05-10
    • 1970-01-01
    • 2016-09-01
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 2013-12-07
    • 1970-01-01
    • 2013-09-11
    相关资源
    最近更新 更多