如何访问gmail使用python?(how to access gmail use python?)

编程入门 行业动态 更新时间:2024-10-25 06:25:49
如何访问gmail使用python?(how to access gmail use python?)

我想访问gmail使用python和oauth2.0,所以我从“ http://google-mail-oauth2-tools.googlecode.com/svn/trunk/python/oauth2.py ”下载了oauth2.py。 这是我的演示:

import oauth2 import imaplib import email from oauth2client.client import OAuth2WebServerFlow from launchpadlib.credentials import access_token_page email = 'karlvorndoenitz@gmail.com' client_id = 'client_id' client_secret = 'client_secret' # Check https://developers.google.com/drive/scopes for all available scopes OAUTH_SCOPE = 'https://mail.google.com/' # Redirect URI for installed apps REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob' flow = OAuth2WebServerFlow(client_id, client_secret, OAUTH_SCOPE, REDIRECT_URI) authorize_url = flow.step1_get_authorize_url() print 'Go to the following link in your browser: ' + authorize_url authorization_code = raw_input("Please input the code:").strip() response = oauth2.AuthorizeTokens(client_id, client_secret, authorization_code) access_token = response['access_token'] auth_string = oauth2.GenerateOAuth2String(email,access_token,base64_encode=True) print auth_string imap_conn = imaplib.IMAP4_SSL('imap.gmail.com') imap_conn.debug = 4 imap_conn.authenticate('XOAUTH2',lambda x:auth_string) imap_conn.select('INBOX')

但该演示有一些错误,我不知道如何调试它。来自控制台的信息:

16:58.52 > KOMN1 AUTHENTICATE XOAUTH2 16:58.79 < + 16:58.79 write literal size 204 16:59.27 < + eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ== 16:59.27 write literal size 204 16:59.27 < KOMN1 NO Invalid SASL argument. d10if1169757igr.56 16:59.27 NO response: Invalid SASL argument. d10if1169757igr.56 Traceback (most recent call last): File "/home/karl/workspace/Gmail/download_gmail/download_gmail_api.py", line 33, in <module> imap_conn.authenticate('XOAUTH2',lambda x:auth_string) File "/usr/lib/python2.7/imaplib.py", line 351, in authenticate raise self.error(dat[-1]) imaplib.error: Invalid SASL argument. d10if1169757igr.56

我需要帮助。

I wanna access gmail use python and oauth2.0,so I download the oauth2.py from "http://google-mail-oauth2-tools.googlecode.com/svn/trunk/python/oauth2.py". And this is my demo:

import oauth2 import imaplib import email from oauth2client.client import OAuth2WebServerFlow from launchpadlib.credentials import access_token_page email = 'karlvorndoenitz@gmail.com' client_id = 'client_id' client_secret = 'client_secret' # Check https://developers.google.com/drive/scopes for all available scopes OAUTH_SCOPE = 'https://mail.google.com/' # Redirect URI for installed apps REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob' flow = OAuth2WebServerFlow(client_id, client_secret, OAUTH_SCOPE, REDIRECT_URI) authorize_url = flow.step1_get_authorize_url() print 'Go to the following link in your browser: ' + authorize_url authorization_code = raw_input("Please input the code:").strip() response = oauth2.AuthorizeTokens(client_id, client_secret, authorization_code) access_token = response['access_token'] auth_string = oauth2.GenerateOAuth2String(email,access_token,base64_encode=True) print auth_string imap_conn = imaplib.IMAP4_SSL('imap.gmail.com') imap_conn.debug = 4 imap_conn.authenticate('XOAUTH2',lambda x:auth_string) imap_conn.select('INBOX')

But the demo has some bugs,I don't know how to debug it.The information from console:

16:58.52 > KOMN1 AUTHENTICATE XOAUTH2 16:58.79 < + 16:58.79 write literal size 204 16:59.27 < + eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ== 16:59.27 write literal size 204 16:59.27 < KOMN1 NO Invalid SASL argument. d10if1169757igr.56 16:59.27 NO response: Invalid SASL argument. d10if1169757igr.56 Traceback (most recent call last): File "/home/karl/workspace/Gmail/download_gmail/download_gmail_api.py", line 33, in <module> imap_conn.authenticate('XOAUTH2',lambda x:auth_string) File "/usr/lib/python2.7/imaplib.py", line 351, in authenticate raise self.error(dat[-1]) imaplib.error: Invalid SASL argument. d10if1169757igr.56

I need help.

最满意答案

auth_string不应该是base64encoded 。 我相信IMAP4.authenticate会为您编码。 无论如何它对我有用。

例如: auth_string = 'user=%s\1auth=Bearer %s\1\1' % (user, access_token)

The auth_string should not be base64encoded. I believe the IMAP4.authenticate encodes it for you. It worked for me anyways.

For example: auth_string = 'user=%s\1auth=Bearer %s\1\1' % (user, access_token)

更多推荐

本文发布于:2023-08-05 23:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1441139.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:gmail   python   access

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!