# 第三方网站免登
## 概述
身份验证为应用 web 系统提供了获取正在访问用户Lark身份的能力，便于应用 web 系统构建登录流程。
## 整体流程
第一步: 网页后端发现用户未登录，[请求授权登录](https://open.larksuite.com/document/common-capabilities/sso/api/obtain-oauth-code)；<br>
第二步: 用户登录后，开放平台生成登录预授权码，302跳转至重定向地址；<br>
第三步: 网页后端调用[获取user_access_token](https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/authen-v1/oidc-access_token/create)校验登录预授权码合法性，获取user_access_token；<br>
第四步: 如需其他用户信息，网页后端可调用[获取用户信息（身份验证）](https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/authen-v1/authen/user_info)。
### 浏览器内网页登录
Lark客户端内，无需用户进行扫码登录或账号密码登录。

![image.png](//sf16-sg.larksuitecdn.com/obj/open-platform-opendoc-sg/c28979637300e5e09abd0ed74bc72b1d_S2mWoM9tzN.png?lazyload=true&width=925&height=397)

## 示例
### 1. 创建应用
进入[开发者后台](https://open.larksuite.com/app)，点击「创建自建应用」，获取 app_id 和 app_secret。
### 2. 配置应用重定向 URL
在应用的「安全域名」配置中添加重定向 URL，例如 https://open.larksuite.com/document。若未配置或者配置不正确开放平台会提示请求非法。

![image.png](//sf16-sg.larksuitecdn.com/obj/open-platform-opendoc-sg/5fd31bf359f0b258a36d24cf22beccd3_cYkIb3x2G9.png?lazyload=true&width=1640&height=703)
### 3. 请求授权登录
根据[请求授权登录](https://open.larksuite.com/document/common-capabilities/sso/api/obtain-oauth-code)接口说明组装请求 URL，跳转至该URL，开启身份验证流程。
特别地，redirect_uri 参数需要经过 [URLEncode](https://meyerweb.com/eric/tools/dencoder/) 处理，例如：<br>
https://open.larksuite.com/open-apis/authen/v1/authorize?app_id=cli_a219a18a64f8d01b&redirect_uri=https%3A%2F%2F127.0.0.1%2Fmock&scope=bitable:app:readonly%20contact:contact&state=RANDOMSTATE

企业未安装应用时，将有如下提示:

![image.png](//sf16-sg.larksuitecdn.com/obj/open-platform-opendoc-sg/a5c030a35f629f10de4783e8a43a8011_1teRCZ2ya6.png?lazyload=true&width=371&height=341)
### 4. 获取登录预授权码
在上一步骤中，用户完成登录后(扫码登录或账号登录)，开放平台会将 code 和 state 作为参数拼接在 redirect_uri  并重定向请求该地址，如下所示:

```shell 
https://127.0.0.1/mock?code=59bg7a22c77447bbb4c92bbd675954gg&state=RANDOMSTATE  
``` 

### 5. 获取user_access_token
本步骤在Postman上请求登录身份验证接口（详见[获取user_access_token](https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/authen-v1/oidc-access_token/create)），用 code user_access_token、refresh_token。请注意，code 有效期为5分钟，且只能使用一次。

**请求头 ：**

名称 | 类型 | 必填 | 描述
---|---|---|---
Authorization | string | 是 | `app_access_token`<br>**值格式**："Bearer `access_token`"<br>**示例值**："Bearer a-7f1bcd13fc57d46bac21793a18e560"<br>[了解更多：获取与使用access_token](https://open.larksuite.com/document/ukTMukTMukTM/uMTNz4yM1MjLzUzM)
Content-Type | string | 是 | **固定值**："application/json; charset=utf-8"

**请求 Body ：**

```js 
{
    "grant_type":"authorization_code",
    "code":"xMSldislSkdK"
} 
```
**返回 Body ：**
```js
{ 
    "code": 0,
    "msg": "success",
    "data": {
        "access_token": "u-5Dak9ZAxJ9tFUn8MaTD_BFM51FNdg5xzO0y010000HWb",
        "refresh_token": "ur-6EyFQZyplb9URrOx5NtT_HM53zrJg59HXwy040400G.e",
        "token_type": "Bearer",
        "expires_in": 7199,
        "refresh_expires_in": 2591999,
        "scope": "auth:user.id:read bitable:app"
    }
}
```

### 6. 获取用户信息
上一步骤中将会返回用户 access_token 和 refresh_token，并有对应的有效时长字段返回。利用步骤5中获取的access_token[获取用户信息（身份验证）](https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/authen-v1/authen/user_info)，敏感信息在获取企业授权后才返回。

![image.png](//sf16-sg.larksuitecdn.com/obj/open-platform-opendoc-sg/8609b93737a70abd91ec4ad843ecf09e_1A05hKYjbC.png?lazyload=true&width=1280&height=527)
### 7. 刷新user_access_token
获取的user_access_token和refresh_token均存在有效期，前者有效期较短，后者较长。在user_access_token过期后，可根据refresh_token调用[刷新 user_access_token](https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/authen-v1/oidc-refresh_access_token/create)获取新的 user_access_token 和 refresh_token，新 user_access_token 和 refresh_token 有效期与初次生成一致。若刷新失败，可在过期时间内重试。  