> This page is for 平台, version 旧版.
> For other versions, use one of these documentation indexes:
> - V4 (default): https://next.developer.frame.io/platform/v4/llms.txt
> - V4 实验版: https://next.developer.frame.io/platform/v4-experimental/llms.txt
> - 旧版: https://next.developer.frame.io/platform/v2/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://next.developer.frame.io/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://next.developer.frame.io/_mcp/server.

# 构建 OAuth 2 应用程序

<Warning title="本指南假定您已经注册了一个 OAuth 2 应用程序">
  如果尚未注册，请参考 [OAuth 2 授权代码流程](/oauth-2-applications/oauth-2-code-authorization-flow)来配置您的应用程序。
</Warning>


## 应用程序基础知识

**注意**：您应当安全地存储 `client_id` 和（如果不使用 PKCE）`client_secret`，并通过您的环境访问它们。以下示例假设存在一个 **.env** 文件，其中包含变量 `CLIENT_ID` 和 `CLIENT_SECRET`。

**`Python`**

```python title="Python"
import urllib, requests, requests.auth
import os

CLIENT_ID = os.environ.get('CLIENT_ID')
CLIENT_SECRET = os.environ.get('CLIENT_SECRET')

AUTHORIZE_URL = "https://applications.frame.io/oauth2/auth"
TOKEN_URL = "https://applications.frame.io/oauth2/token"
# The scopes you've chosen for your app, space-delimited
SCOPE = "offline account.read asset.read" 
# The callback URI for your app
REDIRECT_URI = "https://yourapp.domain/callback"
```





## 调用认证服务器




首先，您的应用程序需要调用 Frame.io 认证服务器，然后该服务器会将用户重定向到登录页面。





**`Python`**

```python title="Python"
import uuid
from urllib.parse import urlencode

def create_auth_url():
 credentials = {
  'response_type': 'code',
  'redirect_uri': REDIRECT_URI,
  'client_id': CLIENT_ID,
  'scope': SCOPE,
  'state': str(uuid.uuid4())
 }
 url = (AUTHORIZE_URL + "?" + urlencode(credentials))
 return url
```





## 回调

然后，认证服务器会向您的 `REDIRECT_URI` 发起一个 `GET` 请求，而该 URI 随后需要调用 `TOKEN_URL`。此回调会根据您的应用程序是否配置为使用 PKCE 而略有不同。

### 不使用 PKCE

如果您不使用 PKCE，您的回调必须包含一个 `Authorization` 标头，其中包括您的 `CLIENT_ID` 和 `CLIENT_SECRET`。

**`Python`**

```python title="Python"
def callback():
   state = request.args.get('state')
   scope = request.args.get('scope')
   code = request.args.get('code')
   error = request.args.get('error')

   if error:
     return "Error: " + error

   # Set up for client authorization and set up the data you need to send.
   client_auth = requests.auth.HTTPBasicAuth(CLIENT_ID, CLIENT_SECRET)

   post_data = {
     "grant_type": "authorization_code",
     "code": code,
     "redirect_uri": REDIRECT_URI,
     "state": state,
     "scope": SCOPE
   }

# Send a POST request with the data you need to receive an access token. 
# If everything goes well, it will be returned to you and you can use it with
# Frame.io.

   response = requests.post(TOKEN_URL, auth=client_auth, data=post_data)    
   return response.text
```





### 使用 PKCE

如果您使用 PKCE，您的回调 *不得* 包含 `Authorization` 标头，但在回调 `TOKEN_URL` 时，*必须* 在`POST` 请求体中包含您的 `CLIENT_ID`。

**`Python`**

```python title="Python"
def callback():
   state = request.args.get('state')
   scope = request.args.get('scope')
   code = request.args.get('code')
   error = request.args.get('error')

   if error:
     return "Error: " + error

# If using PKCE, you must include the CLIENT_ID in your request body  
   post_data = {
     "grant_type": "authorization_code",
     "code": code,
     "redirect_uri": REDIRECT_URI,
     "state": state,
     "scope": SCOPE
     "client_id": CLIENT_ID 
   }

# Send a POST request with the data you need to receive an access token. 
# If everything goes well, it will be returned to you and you can use it with
# Frame.io

  # If using PKCE, use the below request with no auth
  response = requests.post(TOKEN_URL, data=post_data) 

   return response.text
```





## 成功响应




如果您的回调成功，则会收到一个类似如下的 JSON 响应：




```json
{
  "access_token":"BEARER_TOKEN",
  "expires_in":3600,
  "refresh_token":"REFRESH_TOKEN",
  "scope":"account.read offline",
  "token_type":"bearer"
}
```

现在，您可以使用 `access_token` 代表已登录用户向 Frame.io 发起 API 调用，并可以使用 `refresh_token` 在此令牌过期后请求新的 `access_token`。