> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fluz.app/llms.txt
> Use this file to discover all available pages before exploring further.

# 交换 OAuth 代码

## 将授权代码换取 Access Token

你可以使用权限请求返回的代码发起请求，交换获得 Access Token 和 Refresh Token。

要将你的授权 `code` 交换为访问令牌，请向 `https://uni.staging.fluzapp.com/token/exchange` 发送请求，并携带以下查询参数：

| query param   | description                             |
| ------------- | --------------------------------------- |
| code          | 上述授权令牌                                  |
| redirect\_uri | 你在上一步授权中使用的 redirect\_uri。该 URI 必须完全匹配。 |

另外，设置一个 Authorization 请求头，其值为将你的 client\_id:app\_secret 组合后进行 base64 编码得到的字符串。该请求头为 Basic 认证，需遵循以下格式：`Authorization: Basic <base64 encoded CLIENT_ID:CLIENT_SECRET>`

例如，如果你的 `client_id` 是 `abc123`，而 OAuth 配置中的 `client_secret` 是 `def456`，则 base64 编码值为 `YWJjMTIzOmRlZjQ1Ng==`。

`client_id` 和 `client_secret` 可在 [配置 OAuth 应用](/configure-o-auth-app) 的 `Overview` 选项卡中找到。

以下是一个 cURL 示例命令：

```text theme={null}
curl -X GET "https://uni.staging.fluzapp.com/token/exchange?code=<the auth code>&redirect_uri=<the redirect_uri you used in the auth step>" -H "Authorization: Basic YWJjMTIzOmRlZjQ1Ng"
```

响应将包含：

| query param  | description                   |
| :----------- | :---------------------------- |
| accessToken  | 用于随后请求 Fluz 后端服务的短期令牌         |
| refreshToken | 在 accessToken 过期后需要存储并使用的刷新令牌 |
| scopes       | 用户在先前流程中授权的值                  |

下面是一个完整响应的示例：

```json theme={null}
{"accessToken":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTc1MjA5NDgwNH0.0dKCtVpN0mTHHMuGNNx4VLJisTnovFNPQKhSw6zWosc","authorizationCode":"f082972eb110b80f73b0d0f95d1de9266069a1e4","accessTokenExpiresAt":"2025-07-08T21:05:30.673Z","refreshToken":"8ec16c25951616150b0332a4a6d66547","refreshTokenExpiresAt":"2025-08-08T20:55:30.738Z","scope":\["MAKE\_WIDTHDRAW","MAKE\_DEPOSIT","LIST\_PAYMENT"],"client":\{"id":"dab5c80e-0321-4c3a-988a-ffedfd64d8db","app\_id":"0a92d46e-edf4-422e-8c62-946051e5067b","app\_name":"First OAuth integration","grants":\["authorization\_code","client\_credentials","password","refresh\_token"],"redirectUris":\["http\://localhost:3035/oauth/finalize"],"accessTokenLifetime":600},"user":\{"id":"5070d5a1-d71a-4190-91b0-f116eec51771"}}
```
