2014-02-14 1 views
3

Gmail에 파이썬과 oauth2.0을 사용하고 싶기 때문에 "http://google-mail-oauth2-tools.googlecode.com/svn/trunk/python/oauth2.py"에서 oauth2.py를 다운로드합니다. 내가 콘솔에서 좋았지 정보를 디버깅하는 방법을 모른다,Gmail에 Python을 사용하는 방법에 액세스 하시겠습니까?

import oauth2 
import imaplib 
import email 
from oauth2client.client import OAuth2WebServerFlow 
from launchpadlib.credentials import access_token_page 

email = '[email protected]' 
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 

내가 도움이 필요합니다.

+0

[이 페이지 (http://code.google.com/p/google-mail-oauth2-tools/wiki/OAuth2DotPyRunThrough) – goncalopp

+0

HTTPS 유용 할 수있다 : 예를 들어

// 개발자 .google.com/api-client-library/python/guide/aaa_oauth 링크 된 @goncalopp에이 페이지를 추가하겠습니다. 달력에는 이해할 수 있도록 많은 설명이 있습니다. – DaImTo

+0

imap을 사용하여 이메일에 액세스하려면 oauth가 필요하다고 생각하지 않습니다. – User

답변

1

auth_stringbase64encoded이 아니어야합니다. 나는 IMAP4.authenticate이 당신을 위해 그것을 암호화한다고 믿습니다. 그것은 어쨌든 나를 위해 일했습니다. 가 auth_string = 'user=%s\1auth=Bearer %s\1\1' % (user, access_token)

관련 문제