Vai alla navigazione

Creazione di un'app OAuth 2

Questa guida presume che tu abbia già registrato un'app OAuth 2

In caso contrario, consulta Flusso del codice OAuth 2 per configurare l’applicazione.

Nozioni di base per l’applicazione

Nota: il client_id e (se non usi PKCE) il client_secret devono essere archiviati in modo sicuro e accessibili tramite l’ambiente. Gli esempi seguenti presuppongono la presenza di un file .env contenente le variabili CLIENT_ID e CLIENT_SECRET.

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"

Chiamata del server di autenticazione

Prima di tutto, l’applicazione deve chiamare il server di autenticazione Frame.io, che poi reindirizza l’utente a una pagina di accesso.

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

Il callback

Il server di autenticazione eseguirà quindi una richiesta GET al REDIRECT_URI, che a sua volta dovrà chiamare il TOKEN_URL. Questo callback sarà leggermente diverso a seconda che l’applicazione sia configurata o meno in modo da usare PKCE.

Senza PKCE

Se non usi PKCE, il callback deve includere un’intestazione Authorization che includa il CLIENT_ID e il CLIENT_SECRET.

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

Con PKCE

Se usi PKCE, il callback non deve includere un’intestazione Authorization, ma deve includere il CLIENT_ID nel corpo della richiesta POST quando chiama il TOKEN_URL.

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

Risposta di operazione riuscita

Se il callback riesce, riceverai una risposta JSON simile a questa:

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

Ora puoi usare l’access_token per effettuare chiamate API a Frame.io per conto dell’utente connesso e il refresh_token per richiedere un nuovo access_token dopo la scadenza di questo token.