Oauth2授权模式访问之授权码模式(authorization_code)访问

Oauth2授权模式访问之授权码模式(authorization_code)访问

获取code

redirect_uri可以随便写,在浏览器输入(注意是get请求方式):

http://localhost:8080/oauth/authorize?response_type=code&client_id=pair&redirect_uri=http://baidu.com
  • 1

这里写图片描述
如果没用登陆,会首先重定向到登陆页面:

这里写图片描述

授权(authrization)

这个过程需要确保用户是已经登陆的情况,在浏览器中输入和步骤1一样的地址(注意是get请求方式):

http://localhost:8080/oauth/authorize?response_type=code&client_id=pair&redirect_uri=http://baidu.com
  • 1

此时会出现让用户授权的页面:

这里写图片描述

如果拒绝,则会重定向到redirect_uri的地址:

这里写图片描述

如果同意授权,跳转到redirect_uri地址,并且在uri后面追加返回的code,如https://www.baidu.com/?code=o4YrCS:

这里写图片描述

根据code获取access_token

在上一步中拿到的code,通过POSTMAN发送post请求,grant_typeauthorization_code,请求地址如下:

http://localhost:8080/oauth/token?
grant_type=authorization_code
&code=o4YrCS&client_id=pair
&client_secret=secret&redirect_uri=http://baidu.com
  • 1
  • 2
  • 3
  • 4

这里写图片描述

返回access_token格式:

{
    "access_token": "a8ae6a78-289d-4594-a421-9b56aa8f7213",
    "token_type": "bearer",
    "expires_in": 1999,
    "scope": "read write trust"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

根据access_token获取资源

访问http://localhost:8080/rest/api/ping?access_token=a8ae6a78-289d-4594-a421-9b56aa8f7213:

这里写图片描述

返回资源结果:

{
    "key": "Hello Oauth2"
}
  • 1
  • 2
  • 3

刷新token(refresh_token)

http://localhost:8080/oauth/token?
        grant_type=refresh_token
        &refresh_token=ce3dd10e-ec60-4399-9076-ee2140b04a61
        &client_id=pair&client_secret=secret
  • 1
  • 2
  • 3
  • 4

这里写图片描述

返回结果:

{
  "access_token": "436423b4-fc22-4f41-8186-d8706ae9396f",
  "token_type": "bearer",
  "refresh_token": "ce3dd10e-ec60-4399-9076-ee2140b04a61",
  "expires_in": 1999,
  "scope": "read write trust"
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于 Python,你可以使用第三方库来实现 OAuth2 授权。其中最常用的库是 `requests-oauthlib`。以下是一个简单的示例代,展示了如何使用该库进行 OAuth2 授权: ```python from requests_oauthlib import OAuth2Session # 定义授权参数 client_id = 'YOUR_CLIENT_ID' client_secret = 'YOUR_CLIENT_SECRET' authorization_base_url = 'AUTHORIZATION_URL' token_url = 'TOKEN_URL' redirect_uri = 'REDIRECT_URI' # 创建 OAuth2Session 对象 oauth = OAuth2Session(client_id, redirect_uri=redirect_uri) # 生成授权 URL authorization_url, _ = oauth.authorization_url(authorization_base_url) # 打印授权 URL,并在浏览器中访问进行授权 print(f'请在浏览器中访问以下 URL 进行授权: {authorization_url}') # 获取授权 authorization_code = input('请输入授权: ') # 使用授权获取访问令牌 token = oauth.fetch_token( token_url, code=authorization_code, client_secret=client_secret ) # 使用访问令牌发送请求 response = oauth.get('API_ENDPOINT') print(response.json()) ``` 请注意,以上代中的参数需要根据你的实际情况进行修改,包括 `client_id`、`client_secret`、`authorization_base_url`、`token_url`、`redirect_uri` 和 `API_ENDPOINT`。你需要根据你使用的 OAuth2 提供商的文档来获取这些参数。 此外,还有其他一些流行的 Python OAuth2 库可以使用,如 `authlib`、`python-oauth2` 等,你可以根据自己的需求选择适合你的库来实现 OAuth2 授权

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值