使用XOAUTH访问Gmail收件箱 [英] IMAP access to Gmail inbox with XOAUTH

查看:111
本文介绍了使用XOAUTH访问Gmail收件箱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循示例,我已经授权了我的应用程序(=获得了访问令牌),但试图访问收件箱的结果如下:

  21:14.12> NDIB1 AUTHENTICATE XOAUTH 
21:14.17< +
21:14.17写文字大小480
21:14.74< NDIB1 NO [ALERT]无效凭证(失败)
21:14.74无回应:[ALERT]凭证无效(失败)

这是代码:

 导入xoauth 
将oauth2导入为oauth
进口imaplib

=消费oauth.Consumer( '匿名', '匿名')
=的access_token oauth.Token(my_access_token,my_access_token_secret)
用户= my_gmail_address
原=imap#是吗?
xoauth_requestor_id =用户#和这个?
xoauth_string = xoauth.GenerateXOauthString(consumer,access_token,user,proto,xoauth_requestor_id,'','')
conn = imaplib.IMAP4_SSL('imap.googlemail.com')
conn。调试= 4
conn.authenticate( 'XOAUTH',拉姆达X:xoauth_string)
conn.select( '收件箱')
打印conn.list()

它在conn.authenticate()调用时失败。任何想法?上面的例子使用匿名进行身份验证,但是,您必须设置消费者密钥和消费者密钥到您已在管理域名控制面板中设置的值:

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


I'm following the example, I already have authorized my application (=obtained the access token) but trying to access the inbox results in this:

  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)

This is the code:

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()

It fails on the conn.authenticate() call. Any idea?

解决方案

The example above uses "anonymous" for authentication but, you must set "Consumer Key" and "Consumer Secret" to the values you've already set in "Manage Domains" control panel at:

https://accounts.google.com/ManageDomains

Example:

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

这篇关于使用XOAUTH访问Gmail收件箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆