使用OAuth令牌连接Gmail的IMAP时身份验证失败

4
我正在编写一个脚本,使用谷歌的身份验证登录。我目前获取了访问令牌和用户的电子邮箱地址,并将其传递给我的函数,该函数使用imap连接到gmail,然后对邮件执行某些操作。我生成授权字符串的方式类似于我在网上看到的其他人,但是我收到了以下错误消息:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/flask/app.py", line 2000, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1991, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1567, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/Harrison/Desktop/Uber/UberStats.py", line 60, in index
    return Uber_Cost(email_address, access_token)
  File "/Users/Harrison/Desktop/Uber/UberStats.py", line 103, in Uber_Cost
    mail.authenticate('XOAUTH2', lambda x: auth_string)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/imaplib.py", line 364, in authenticate
    raise self.error(dat[-1])
error: [AUTHENTICATIONFAILED] Invalid credentials (Failure)

我会打印出访问代码以及我正在登录的电子邮件地址,以便知道这些值不为空。我生成认证字符串的方式有误吗?我是否没有正确地进行 IMAP 认证?
以下是我的代码:
from flask import Flask, request, url_for, session, redirect, jsonify
from flask_oauth import OAuth
import json
import imaplib
import email
from bs4 import BeautifulSoup
import base64





GOOGLE_CLIENT_ID = '****'
GOOGLE_CLIENT_SECRET = '***'
REDIRECT_URI = '/authorized'  # one of the Redirect URIs from Google APIs console

SECRET_KEY = 'Uber'
DEBUG = True

app = Flask(__name__)
app.secret_key = 'Uber'
oauth = OAuth()

google = oauth.remote_app('google',
                          base_url='https://www.google.com/accounts/',
                          authorize_url='https://accounts.google.com/o/oauth2/auth',
                          request_token_url=None,
                          request_token_params={'scope': 'https://www.googleapis.com/auth/userinfo.email',
                                                'response_type': 'code'},
                          access_token_url='https://accounts.google.com/o/oauth2/token',
                          access_token_method='POST',
                          access_token_params={'grant_type': 'authorization_code'},
                          consumer_key=GOOGLE_CLIENT_ID,
                          consumer_secret=GOOGLE_CLIENT_SECRET)


@app.route('/')
def index():
    access_token = session.get('access_token')
    if access_token is None:
        return redirect(url_for('login'))

    access_token = access_token[0]
    from urllib2 import Request, urlopen, URLError

    headers = {'Authorization': 'OAuth '+access_token}
    req = Request('https://www.googleapis.com/oauth2/v1/userinfo',
                  None, headers)
    try:
        res = urlopen(req)
    except URLError, e:
        if e.code == 401:
            # Unauthorized - bad token
            session.pop('access_token', None)
            return redirect(url_for('login'))
        return res.read()
    j = json.loads(res.read())
    email_address = j['email']
    print email_address, access_token
    return Uber_Cost(email_address, access_token)


@app.route('/login')
def login():
    callback=url_for('authorized', _external=True)
    return google.authorize(callback=callback)



@app.route(REDIRECT_URI)
@google.authorized_handler
def authorized(resp):
    access_token = resp['access_token']
    session['access_token'] = access_token, ''
    return redirect(url_for('index'))


@google.tokengetter
def get_access_token():
    return session.get('access_token')


def GenerateOAuth2String(username, access_token, base64_encode=True):
    auth_string = 'user=%s\1auth=Bearer %s\1\1' % (username, access_token)
    if base64_encode:
        auth_string = base64.b64encode(auth_string)
    return auth_string




def Uber_Cost(email_address, access_token):


    auth_string = GenerateOAuth2String(email_address, access_token, base64_encode=False)



    mail = imaplib.IMAP4_SSL('imap.gmail.com')
    mail.debug = 4
    mail.authenticate('XOAUTH2', lambda x: auth_string)
    mail.select('INBOX')

你应该看一下这个答案:https://dev59.com/Fm435IYBdhLWcg3w0Trc#5366380 - Jake Conway
@JakeConway 那是oauth2。我认为oauth1没有用户秘密令牌。我该怎么办才能获得它? - Harrison
看起来您已经拥有了Oauth2令牌。您需要一个access_token来进行连接。 - Max
@Max,我该如何连接? - Harrison
创建一个 imaplib.IMAP_SSL 对象? - Max
显示剩余2条评论
1个回答

0

根据您最新的代码,看起来您已经掌握了authenticate()方法。

您还需要https://mail.google.com/ OAuth范围以通过IMAP和SMTP服务器进行身份验证。

因此,请将其添加到您的范围请求中,并确保您的应用程序已在Google应用程序控制台上配置为此范围:

request_token_params={'scope': 'https://www.googleapis.com/auth/userinfo.email https://mail.google.com/',
                                            'response_type': 'code'},

Google OAUTH2协议和范围的文档记录在他们的开发者页面上。


代码已更新为我目前拥有的内容。那应该放在哪里?请求令牌参数中吗? - Harrison
如果我将 Gmail 添加到作用域中,语法会是什么样子? - Harrison
成功了!非常感谢!你知道我如何使用我自己购买的自定义域名而不是使用localhost吗? - Harrison
我会发布一个新问题。 - Harrison
1
我已经授予您赏金,谢谢。这是我发布的另一个问题:http://stackoverflow.com/questions/40685275/use-custom-domain-for-flask-app-with-google-authenticated-login - Harrison
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接