1

大家好,

烧瓶和 authlib 有一些问题。我的闪存代码的波纹管截图

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'

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

谢谢你。

托比

更新:根据下面的评论,最新代码:

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 函数在不带参数的情况下调用时抛出错误。这很奇怪,因为我发现的大多数例子都是这样做的。client_id 和 client_secret 通过 config.py 文件设置。这适用于 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中的调试没有运气,非常感谢帮助!

4

1 回答 1

0

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

在此处查看示例:

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

松弛的例子有同样的问题。

于 2019-11-20T13:51:10.227 回答