> This page is for Plataforma, version Herdado.
> For other versions, use one of these documentation indexes:
> - V4 (default): https://next.developer.frame.io/platform/v4/llms.txt
> - V4 experimental: https://next.developer.frame.io/platform/v4-experimental/llms.txt
> - Herdado: 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.

# Criação de um aplicativo OAuth 2

<Warning title="Este guia pressupõe que você já tenha registrado um aplicativo OAuth 2.">
  Caso contrário, consulte o [Fluxo de código do OAuth 2](/oauth-2-applications/oauth-2-code-authorization-flow) para configurar seu aplicativo.
</Warning>


## Noções básicas sobre o aplicativo

**Observação**: você deve armazenar seu `client_id` e (caso não esteja usando PKCE) seu `client_secret` de forma segura e acessá-los por meio do seu ambiente.Os exemplos abaixo pressupõem a existência de um arquivo **.env** contendo as variáveis `CLIENT_ID` e `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"
```





## Chamando o servidor de autenticação




Primeiro, seu aplicativo precisará chamar o servidor de autenticação do Frame.io, que, em seguida, redirecionará o usuário para uma página de login.





**`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
```





## O callback

O servidor de autenticação fará uma solicitação `GET` para o seu `REDIRECT_URI`, que por sua vez precisará chamar o `TOKEN_URL`.Esse callback será ligeiramente diferente dependendo se o seu aplicativo está ou não configurado para usar PKCE.

### Sem PKCE

Se você não estiver usando PKCE, seu callback deve incluir um cabeçalho `Authorization` que contenha seu `CLIENT_ID` e `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
```





### Com PKCE

Se você estiver usando PKCE, seu callback *não* deve incluir um cabeçalho `Authorization` mas *deve* incluir seu `CLIENT_ID` no corpo da solicitação `POST` ao fazer o callback para o `TOKEN_URL`.

**`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
```





## Resposta bem-sucedida




Se o seu callback for bem-sucedido, você receberá uma resposta JSON semelhante a esta:




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

Agora você pode usar o `access_token` para fazer chamadas de API ao Frame.io em nome do usuário conectado e o `refresh_token` para solicitar um novo `access_token` após a expiração deste token.