OAuth 2 토큰 새로 고침

이 가이드는 OAuth 2 앱을 이미 구축했다고 가정합니다.

아직 구축하지 않은 경우 가이드를 참조하고, 성공적인 OAuth 2 자격 증명 권한 부여를 통해 access_tokenrefresh_token을 확보한 후 다시 여기로 돌아오세요.

토큰 새로 고침의 기본

OAuth 2.0 자격 증명 요청에 offline 권한을 포함했다고 가정할 때, Frame.io Accounts 애플리케이션을 통한 인증에 성공하면 다음과 같은 페이로드가 반환됩니다.

1{
2 "access_token":"BEARER_TOKEN",
3 "expires_in":3600,
4 "refresh_token":"REFRESH_TOKEN",
5 "scope":"account.read offline",
6 "token_type":"bearer"
7}

access_token은 인증된 사용자를 대신하여 작업하는 데 사용할 수 있는 베어러 토큰으로, 3600초(1시간) 후에 만료됩니다. 그 후에는 refresh_token을 사용하여 새 access_token을 가져올 수 있습니다. 새로 고침 토큰은 30일 후에 만료되며, 이 시점이 되면 사용자가 처음부터 다시 로그인하여 새로운 액세스/새로 고침 토큰 쌍을 생성해야 합니다. offline 권한을 명시적으로 요청하지 않으면 refresh_token을 수신할 수 없으므로, 1시간 후에 사용자를 완전히 재인증해야 합니다.

인증 성공 시 새로 고침 토큰 캡처하기

당연한 이야기지만, 보유하지 않은 refresh_token은 사용할 수 없으므로 앱에서 다음 사항을 반드시 확인하세요.

  • offline 권한 요청
  • 성공적인 콜백에서 반환된 refresh_token 캡처

편의를 위해 OAuth 2 앱 가이드의 콜백을 새로 고침 토큰을 보관하기 위한 os 호출과 함께 아래에 재현해 두었습니다. PKCE가 구성된 예시(기본 인증 헤더 미포함)와 구성되지 않은 예시(기본 인증 헤더 포함)의 두 가지 예시가 제공된다는 점에 유의하세요.

PKCE 미사용

Python
1def callback():
2 # Where `request` refers to our initial call to the auth URL
3 state = request.args.get('state')
4 scope = request.args.get('scope')
5 code = request.args.get('code')
6 error = request.args.get('error')
7
8 if error:
9 return "Error: " + error
10
11 # Set up for client authorization and set up the data you need to send.
12 client_auth = requests.auth.HTTPBasicAuth(CLIENT_ID, CLIENT_SECRET)
13
14 post_data = {
15 "grant_type": "authorization_code",
16 "code": code,
17 "redirect_uri": REDIRECT_URI,
18 "state": state,
19 "scope": SCOPE
20 }
21
22 # Send a POST request with the data you need to receive an access token.
23 response = requests.post(TOKEN, auth=client_auth, data=post_data)
24 # Stash the refresh token for later
25 os.environ['REFRESH_TOKEN'] = response.json()["refresh_token"]
26
27 return response.text

PKCE 사용

Python
1def callback():
2 # Where `request` refers to our initial call to the auth URL
3 state = request.args.get('state')
4 scope = request.args.get('scope')
5 code = request.args.get('code')
6 error = request.args.get('error')
7
8 if error:
9 return "Error: " + error
10
11 # If using PKCE, you must include the CLIENT_ID in your request body
12 post_data = {
13 "grant_type": "authorization_code",
14 "code": code,
15 "redirect_uri": REDIRECT_URI,
16 "state": state,
17 "scope": SCOPE
18 "client_id": CLIENT_ID
19 }
20
21 # Send a POST request with the data you need to receive an access token.
22 # If using PKCE, use the below request with no auth
23 response = requests.post(TOKEN_URL, data=post_data)
24 # Stash the refresh token for later
25 os.environ['REFRESH_TOKEN'] = response.json()["refresh_token"]
26
27 return response.text

새로 고침 실행

새로 고침 작업 자체는 Frame.io의 토큰 URL에 대한 단일 호출입니다.

새로 고침 시 양식 데이터에 항상 최소한 다음 세 가지 속성이 포함됩니다.

  • grant_type: refresh_token
  • scope: <scopes>
  • refresh_token: <refresh_token>

PKCE를 사용 중인 경우 이 양식 데이터에 앱의 client_id를 포함해야 합니다. 사용하지 않는 경우, Basic Authentication 헤더를 포함하고 앱의 client_idclient_secret을 각각 Username과 Password로 지정해야 합니다.

PKCE 미사용

PKCE 없이 초기 인증 콜백을 수행하는 것과 유사하게, 이 표준 새로 고침 작업도 Basic Authentication 헤더의 Username과 Password에 client_idclient_secret을 각각 제공해야 합니다.

Python
1def refresh():
2 # Fetch the refresh token, assuming we have it
3 REFRESH_TOKEN = os.environ.get('REFRESH_TOKEN')
4
5 client_auth = requests.auth.HTTPBasicAuth(CLIENT_ID,CLIENT_SECRET)
6 post_data = {
7 "grant_type": "refresh_token",
8 "scope": SCOPE,
9 "refresh_token": REFRESH_TOKEN
10 # if using PKCE, you will need to include your client_id as below
11 # "client_id": CLIENT_ID
12 }
13
14 response = requests.post(TOKEN_URL, auth=client_auth, data=post_data)
15 # Catch + stash a new Refresh Token
16 os.environ['REFRESH_TOKEN'] = response.json()["refresh_token"]
17
18 return response.text

PKCE 사용

다시 한 번 강조하자면, 초기 /callback 주기의 규칙을 그대로 따르고 있습니다.

  • Authorization 헤더를 포함하지 않습니다.
  • 페이로드에 client_id를 포함해야 합니다.
Python
1def refresh():
2 # Fetch the refresh token, assuming we have it
3 REFRESH_TOKEN = os.environ.get('REFRESH_TOKEN')
4
5 post_data = {
6 "grant_type": "refresh_token",
7 "scope": SCOPE,
8 "refresh_token": REFRESH_TOKEN
9 "client_id": CLIENT_ID
10 }
11
12 response = requests.post(TOKEN_URL, data=post_data)
13 # Catch + stash a new Refresh Token
14 os.environ['REFRESH_TOKEN'] = response.json()["refresh_token"]
15
16 return response.text

축하합니다!이제 OAuth 2.0 클라이언트 애플리케이션의 전체 토큰 수명 주기를 처리할 수 있습니다.