大家好,
flask和authlib存在一些问题。我的Flash代码下面的代码
library(dplyr)
df %>%
tidyr::fill(question) %>%
filter(!is.na(answer))
#> id question answer
#> 1 1 Who is your favorite singer? Michael Jackson
#> 2 1 Who is your favorite singer? Taylor Swift
#> 3 1 Who is your favorite singer? Celine Dion
#> 4 2 Who is your favorite actor? Brad Pitt
#> 5 2 Who is your favorite actor? Julia Roberts
#> 6 2 Who is your favorite actor? Hugh Grant
#> 7 3 Who is your favorite athlete? Michael Jordan
#> 8 3 Who is your favorite athlete? Dirk Nowitzki
#> 9 3 Who is your favorite athlete? Mike Tyson
运行上述代码时,出现以下错误:
from flask import Flask, render_template
from authlib.integrations.flask_client import OAuth
import os
app = Flask(__name__)
app._static_folder = os.path.abspath("static")
app.config.from_object('config')
oauth = OAuth(app)
webex = oauth.register(name='WEBEX', redirect_uri='http://webapp.dcloud.cisco.com:5000/AuthWebex', client_kwargs={
'scope': 'spark:all'
} )
config.py
import os
WEBEX_CLIENT_ID='C3a256be511cdf07e19f272960c44a214aec14b727b108e4f10bd124d31d2112c'
WEBEX_CLIENT_SECRET='secret'
WEBEX_ACCESS_TOKEN_URL='https://api.ciscospark.com/v1/access_token'
WEBEX_REDIRECT_URI='http://localhost:5000/AuthWebex'
WEBEX_SCOPE='spark:all'
看了一些例子,做了一些研究,没有运气。找不到任何解决方案...
感谢adv。
Tobi
更新: 每个评论的最新代码如下:
File "/Users/tneumann/PycharmProjects/untitled/venv/lib/python3.7/site-packages/authlib/integrations/flask_client/oauth_registry.py", line 61, in register
self.use_oauth_clients()
File "/Users/tneumann/PycharmProjects/untitled/venv/lib/python3.7/site-packages/authlib/integrations/_client/oauth_registry.py", line 49, in use_oauth_clients
clients = self.AVAILABLE_CLIENTS[name]
KeyError: 'requests'
在没有参数的情况下调用oauth.webex.authorize_access_token函数会引发错误。这很奇怪,因为我发现的大多数示例都确实做到了。 通过config.py文件设置client_id和client_secret。这适用于oauth.register函数,但不适用于authorize_access_token。
另一个问题是,即使带有参数,它也会产生有效的令牌。当我调用get函数时,出现以下错误:
from flask import Flask, render_template, url_for, request
from authlib.integrations.flask_client import OAuth
import os
import requests
app = Flask(__name__)
app._static_folder = os.path.abspath("static")
app.config.from_object('config')
app.secret_key = os.urandom(24)
oauth = OAuth(app)
oauth.register(
'webex',
api_base_url='https://api.ciscospark.com/v1',
authorize_url='https://api.ciscospark.com/v1/authorize',
access_token_url='https://api.ciscospark.com/v1/access_token',
redirect_uri='http://webapp.dcloud.cisco.com:5000/AuthWebex',
scope='spark:all')
@app.route('/')
def main():
"""Entry point; the view for the main page"""
return render_template('main.html')
@app.route('/authorize')
def authorize():
return render_template('authorize.html')
@app.route('/login')
def login():
#redirect_uri = url_for('AuthWebex', _external=True)
redirect_uri = 'http://webapp.dcloud.cisco.com:5000/AuthWebex'
print(redirect_uri)
return oauth.webex.authorize_redirect(redirect_uri)
@app.route('/AuthWebex')
def AuthWebex():
#print(request.__dict__)
token = oauth.webex.authorize_access_token( authorization_response=request.url,
redirect_uri='http://webapp.dcloud.cisco.com:5000/AuthWebex',
client_id='C3a256be511cdf07e19f272960c44a214aec14b727b108e4f10bd124d31d2112c',
client_secret='secret',
)
print("Token: ", token)
resp = oauth.webex.get('https://api.ciscospark.com/v1/people/me')
profile = resp.json()
print(profile)
# do something with the token and profile
return '<html><body><h1>Authenticated</h1></body></html>'
if __name__ == '__main__':
app.run()
这是authorize_access_token函数返回的令牌的格式
File "/Users/tneumann/PycharmProjects/untitled/venv/lib/python3.7/site-packages/requests/models.py", line 317, in prepare
self.prepare_auth(auth, url)
File "/Users/tneumann/PycharmProjects/untitled/venv/lib/python3.7/site-packages/requests/models.py", line 548, in prepare_auth
r = auth(self)
File "/Users/tneumann/PycharmProjects/untitled/venv/lib/python3.7/site-packages/authlib/integrations/requests_client/oauth2_session.py", line 41, in __call__
raise UnsupportedTokenTypeError(description=description)
authlib.integrations._client.errors.UnsupportedTokenTypeError: unsupported_token_type: Unsupported token_type: 'token_type'
仔细阅读了文档,github上的代码以及在pycharm中进行调试的过程,但是运气不佳,我们将不胜感激!
答案 0 :(得分:0)
这里的问题是此AuthWebex不是标准的OAuth服务。响应中没有token_type
。我们可以使用Authlib合规性修复程序对其进行修复:
在此处查看示例:
https://docs.authlib.org/en/latest/client/frameworks.html#compliance-fix-for-oauth-2-0
松弛示例有相同的问题。