问题描述
我对编码很陌生,但这几乎没有记录,所以我需要一些帮助。 我正在构建一个烧瓶应用程序,但我无法让谷歌身份验证流程正常工作。 我使用的是 Pycharm 和 python 3.9 版
我的问题是: 我找不到任何解释如何完成身份验证流程的初学者教程。
我不明白如何通过flask与Google Apis交互。 (我想使用android-management-api)
我确实明白我需要创建一个服务对象,但只有当我可以验证谷歌流程时才有效,而这就是我现在已经卡住了 5 天的地方。
我已经按照 realpython 和 MattButton 的说明进行了操作。 尝试这些说明时,我不断收到错误消息。 现在我得到:
Error: While importing 'app',an ImportError was raised:
Traceback (most recent call last):
File "C:\000 Projects\Applications\PY\flaskProjects\MDM\venv\lib\site-packages\flask\cli.py",line 256,in locate_app
__import__(module_name)
File "C:\000 Projects\Applications\PY\flaskProjects\MDM\app.py",line 5,in <module>
from flask_oauth import OAuth
File "C:\000 Projects\Applications\PY\flaskProjects\MDM\venv\lib\site-packages\flask_oauth.py",line 13,in <module>
from urlparse import urljoin
ModuleNotFoundError: No module named 'urlparse'
Process finished with exit code 2
谁能解释一下我做错了什么
我的代码如下:
from flask import Flask,redirect,url_for,session,request,jsonify,render_template
from datetime import datetime
from flask import Flask,session
from flask_oauth import OAuth
import urllib.parse
# You must configure these 3 values from Google Apis console
# https://code.google.com/apis/console
GOOGLE_CLIENT_ID = ''
GOOGLE_CLIENT_SECRET = ''
REDIRECT_URI = '/authorized' # one of the Redirect URIs from Google Apis console
SECRET_KEY = 'development key'
DEBUG = True
app = Flask(__name__)
app.debug = DEBUG
app.secret_key = SECRET_KEY
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/androidmanagement','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 urllib.request import Request,urlopen
from urllib.error import URLError
headers = {'Authorization': 'OAuth '+access_token}
req = Request('https://www.googleapis.com/oauth2/v1/userinfo',None,headers)
try:
res = urlopen(req)
except URLError as e:
if e.code == 401:
# Unauthorized - bad token
session.pop('access_token',None)
return redirect(url_for('login'))
return res.read()
return res.read()
@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')
@app.route('/home')
def home():
return render_template(
'index.html',title='Home Page',year=datetime.Now().year,)
@app.route('/devices')
def devices():
return render_template(
'devices.html',title='Devices',)
@app.route('/policies')
def policies():
return render_template(
'policies.html',title='Policies',)
@app.route('/enterprises')
def enterprises():
return render_template(
'enterprises.html',title='Enterprises',)
@app.route('/contact')
def contact():
# """Renders the contact page."""
return render_template(
'contact.html',title='Contact',message='ICS-Vertex'
)
@app.route('/about')
def about():
return render_template(
'about.html',title='About',message='Your application description page.'
)
if __name__ == '__main__':
app.run()
解决方法
解决了! 我发现有两个冲突的库都使用了请求名称
- Flask.requests
- 请求