【问题标题】:What is the difference between lepture/authlib OAuth2 Client and Session?lepture/authlib OAuth2 客户端和会话有什么区别?
【发布时间】:2018-10-20 10:26:37
【问题描述】:

我正在实现一个 OAuth2 客户端应用程序来连接到 this 服务器。到目前为止,我已经设法使用 Authlib 的 OAuth2Session 实例获取资源 /api/me 。我已经用OAuthClient 尝试过,但它没有用,尽管 Client 类有像put, post, get, delete 这样的方法,所以我想它适合访问资源。 :/

那么有什么区别,为什么我可以使用 Session 的实例访问 /api/me 而无法使用 Client 的实例?

这是我的代码:

    def api_me_get2(token):
    print("========================================")
    print("Sending 2nd GET request to get protected data of me")

    oauth2_client = OAuthClient(
        client_id='ySFTzBKLo0XTaK2tQL9ls4Fc',
        client_secret='vq8vMZplY4J00FrxKx4ynV2mhmL2zzjMzP1U2bXZPhQRcmJl',
        api_base_url=_url(""),
        access_token_url=_url(f"/oauth/token"),
        authorize_url=_url("/oauth/authorize"),
        client_kwargs={"scope":"profile"},
#        client_kwargs={'scope': 'user:email'},
    )

    new_token = oauth2_client.fetch_access_token();
    print(f"New token \"{new_token}\"")
    # FORM data
    '''
    payload = {
        "token":f"{token}"
    }
    print(f"PAYLOAD=\"{payload}\"")
    r = requests.get(_url(f"/api/me"), data=payload, params=payload)
    print(f"RESPONSE {r.status_code}")
    print(f"r.url={r.url}")
    print(f"r.text={r.text}")
    if r.status_code == 200:
        json = r.json()
        print(f"JSON=\"{json}\"")
    '''
    print("========================================")

def api_me_get3(token):
    print("========================================")
    print("Sending 3rd GET request to get protected data of me")

    oauth2_session = OAuth2Session(
        client_id="ySFTzBKLo0XTaK2tQL9ls4Fc",
        client_secret="vq8vMZplY4J00FrxKx4ynV2mhmL2zzjMzP1U2bXZPhQRcmJl",
        token_endpoint_auth_method=None,
        refresh_token_url=_url("/oauth/revoke"),
        refresh_token_params=None,
        scope="profile",
        redirect_uri=None,
        token=token,
        token_placement='header',
        state=None,
        token_updater=None
    )

    r = oauth2_session.request("GET", _url("/api/me"), withhold_token=False, auth=None)

    print(f"Request: \"{r}\"")

    print(f"RESPONSE {r.status_code}")
    print(f"r.url={r.url}")
    print(f"r.text={r.text}")
    if r.status_code == 200:
        json = r.json()
        print(f"JSON=\"{json}\"")

#    new_token = oauth2_client.fetch_access_token();
#    print(f"New token \"{new_token}\"")
    # FORM data
    '''
    payload = {
        "token":f"{token}"
    }
    print(f"PAYLOAD=\"{payload}\"")
    r = requests.get(_url(f"/api/me"), data=payload, params=payload)
    print(f"RESPONSE {r.status_code}")
    print(f"r.url={r.url}")
    print(f"r.text={r.text}")
    if r.status_code == 200:
        json = r.json()
        print(f"JSON=\"{json}\"")
    '''
    print("========================================")

【问题讨论】:

    标签: rest api oauth-2.0 authlib


    【解决方案1】:

    【讨论】:

    • 我是否正确地认为如果我想发出请求,我必须创建client = OAuthClient(...) 的实例,然后使用client.get()、client.post() 等?因为这对我不起作用。它仅适用于session = OAuth2Session(...)。但是有了会话......我不明白如果我想提出个人请求,为什么需要会话。
    猜你喜欢
    • 2020-01-31
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 2022-01-20
    • 2018-08-01
    • 2017-09-28
    相关资源
    最近更新 更多