[关闭]
@elibinary 2016-10-21T02:22:38.000000Z 字数 1414 阅读 1158

使用 OAuth 2.0 访问薄荷 API

doc


关于 OAuth 2.0 协议规范,请参考 OAuth 2.0

OAuth 2.0 基本流程简介

OAuth 2.0 的流程大致可以简单概括为以下四步:

  1. 应用向服务器发起授权申请
  2. 服务器将用户重定向到授权页,询问用户是否对应用授权
  3. 用户同意授权,服务器重定向到 redirect_uri (您的应用),并附加 authorization_code
  4. 使用 authorization_code 向服务器请求 access_token,使用 access_token 访问需授权的 API

获取 authorization_code

接口地址:

  1. GET /oauth/auth

参数:

参数名 必填 说明
client_id 应用的唯一标识
redirect_uri 用户授权完成后的回调地址,应用可以通过此回调地址获得用户的授权结果。此地址必须与在应用注册时填写的回调地址一致!
scope 此处为默认值
state 表示客户端的当前状态,可以指定任意值,认证服务器授权完成回调时会附加此参数

请求示例:

  1. https://boohee.host.example/oauth/auth?
  2. client_id=6c10ef8b98c158825abd&
  3. redirect_uri=https://example.host/callback

响应内容:

  1. https://example.host/callback?code=9b73a4248

获取 access_token

接口地址:

  1. POST /oauth/access_token

参数:

参数名 必填 说明
client_id 应用的唯一标识
client_secret 应用的唯一标识
redirect_uri 用户授权完成后的回调地址,应用可以通过此回调地址获得用户的授权结果。此地址必须与在应用注册时填写的回调地址一致!
grant_type 可选类型: authorization_code, refresh_token
code 根据 grant_type 的类型,如果 grant_type 为 authorization_code ,则此处为上一步中获得的 authorization_code ,如果 grant_type 为 refresh_token ,则此处为 refresh_token

请求示例:

  1. https://boohee.host.example/oauth/access_token?
  2. client_id=6c10ef8b98c158825abd&
  3. client_secret=78c99ad30629fea48c1ed333&
  4. redirect_uri=https://example.host/callback&
  5. grant_type=authorization_code&
  6. code=a8c04313b31a2806

响应内容:

  1. {
  2. "access_token": "652d85781d9f3668f33b604f45696750",
  3. "token_expired_at": "2016-10-21T10:19",
  4. "refresh_token": "acd8578ss2dfe23g5643b604f456sf75ge0"
  5. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注