clients = self.AVAILABLE_CLIENTS [name] KeyError:“ requests” flask authlib client

问题描述 投票:0回答:1

大家好,

烧瓶和authlib存在一些问题。我的Flash代码下面的代码片段

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'

运行以上代码时,出现以下错误:

  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'

[看过例子并做了一些研究,没有运气。找不到任何解决方案...

感谢提供

Tobi

更新:每个评论的最新代码如下:

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()

oauth.webex.authorize_access_token函数在不带参数的情况下调用时抛出错误。这很奇怪,因为我发现的大多数示例都确实做到了。通过config.py文件设置client_id和client_secret。这适用于oauth.register函数,但不适用于authorize_access_token。

另外的问题是,即使具有参数,它也会产生有效的令牌。当我调用get函数时,出现以下错误:

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'

这是authorize_access_token函数返回的令牌的格式

Token:  {'access_token': 'YWIzNGU3<secret>tNDQ5_PF84_7cc07dbd-<secret>-5877334424fd', 'expires_in': 1209599, 'refresh_token': 'MjU2ZDM4N2Et<secret>ZmItMTg5_PF84_7cc07dbd-<secret>877334424fd', 'refresh_token_expires_in': 7722014, 'expires_at': 1574863645}

通过文档,github上的代码和在pycharm中调试没有任何运气,帮助将不胜感激!

authlib
1个回答
0
投票

这里的问题是此AuthWebex不是标准的OAuth服务。响应没有token_type。我们可以使用Authlib合规性修复程序来修复它:

在此处检查示例:

https://docs.authlib.org/en/latest/client/frameworks.html#compliance-fix-for-oauth-2-0

松弛示例有相同的问题。

© www.soinside.com 2019 - 2024. All rights reserved.