> ## 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` 兌換為 access token，請向 `https://uni.staging.fluzapp.com/token/exchange` 發送帶有以下查詢參數的請求：

| query param   | description                               |
| ------------- | ----------------------------------------- |
| code          | 上述的授權權杖                                   |
| redirect\_uri | 你在前一個授權步驟中使用的 redirect\_uri。此 URI 必須完全相同。 |

另外，設定 Authorization 標頭，其值為你的 client\_id:app\_secret 組合後再以 base64 編碼的字串。這是 Basic auth 標頭，應使用以下格式：`Authorization: Basic <base64 encoded CLIENT_ID:CLIENT_SECRET>`

例如，若你的 `client_id` 為 `abc123`，而 OAuth 設定中的 `client_secret` 為 `def456`，則 base64 編碼後的值為 `YWJjMTIzOmRlZjQ1Ng==`。

`client_id` 與 `client_secret` 可在 [Configure OAuth App](/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"}}
```
