简单的OAuth2客户端。
feedr.oauth2的Python项目详细描述
馈线-oauth2
这是一个Python库,它实现了以下OAuth 2.0流的客户端:
- 授权代码
- 客户端凭据
- 设备代码
- 刷新令牌
{tha1}遵循标准中描述的^ a1}。在
快速启动
授权码
fromfeedr.oauth2.authorization_codeimportClient,PKCEclient=ac.Client(auth_uri='https://authorization-server.com/oauth2/authorize',token_uri='https://authorization-server.com/oauth2/token',client_id='...',client_secret='...',redirect_uri='...',use_pkce=True,)# ------------------# /api/login# ------------------request=client.authorization_request(scope='read+write',redirect_uri=EXTERNAL_URL+'/api/login/collect',)persist_state(request.state,request.pkce)returnredirect(request.auth_uri)# ------------------# /api/login/collect?code=...&state=...# ------------------pkce:PKCE=retrieve_state(state)response=client.authorization_code(code,pkce)print(response)# AccessTokenResponse(access_token='...', token_type='bearer', ...)
客户端凭据
^{pr2}$设备代码
fromfeedr.oauth2.device_codeimportClientclient=Client(device_code_uri='https://authorization-server.com/device/code',token_uri='https://authorization-server.com/oauth2/token',client_id='...',client_secret='...',)request=client.get_device_code()print(f'Visit {request.verification_url} and enter the code {request.user_code}.')token_info=client.poll(request)print(token_info)# AccessTokenResponse(access_token='...', token_type='bearer', ...)
刷新令牌
authorization_code.Client
类有一个refresh_token()
方法,可以刷新
只需提供刷新令牌即可轻松访问令牌。在
new_token=client.refresh_token(token_info.refresh_token)
或者,refresh_token.Client
可用于创建refresh_token
请求。在
client=Client(token_uri='https://authorization-server.com/oauth2/token',refresh_token='...',client_id='...',client_secret='...',)token_info=client.get_token()print(token_info)# AccessTokenResponse(access_token='...', token_type='bearer', ...)
- 项目
标签: