포스트

2026-05-05-aps 코드 구성

사용된 라이브러리

requests : 외부 서버에 HTTP 요청을 보내는 라이브러리 / 왜 필요한가? : 우리 서버에서 Autodesk 서버에 토큰 발급, 파일 업로드, 변환 등을 요청하기 위해 필요함 base64 : 데이터를 안전하게 전송할 수 있는 문자열로 변환해주는 라이브러리 / 왜 필요한가? : 문자열에 특수문자가 포함되어 있으면 데이터 전송과정에서 깨질 수 있음. 데이터 전송에도 문제 없도록 특수문자를 안전한 문자로 변환해 줌

코드 구성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import requets
import base64
from config import CLIENT_ID, CLIENT_SECRET, APS_BUCKET

credentails = f"{CLIENT_ID}:{CLIENT_SECRET}"
authorization = base64.b64encode(credentails.encode()).decode()

def get_internal_token():
	response = requests.post(
		"https://developer.api.autodesk.com/authentication/v2/token",
		headers = {"authorization" : f"Basic {authorization}"},
		data = {
			"grant_type" : "client_credentials",
			"scope" : "data:read data:write date:create bucket:read bucket:create"
		}
	)
	return response.json()

def get_public_token():
	response = requests.post(
		"https://developer.api.autodesk.com/authentication/v2/token",
		headers = {"authorization" : f"Basic {authorization}"},
		data = {
			"grant_type" : "client_credentials",
			"scope" : "viewables:read"
		}
	)
	return response.json()
	
def ensure_bucket():
	token = get_internal_token()["access_token"]
	response = requests.post(
		"https://developer.api.autodesk.com/oss/v2/buckets",
		headers = {"Authorization" : f"Bearer {token}"},
		json = {
			"bucketKey" : APS_BUCKET,
			"policyKey" : "persistent"
		}
	)
	return response.status_code in [200, 409]
	
def get_objects():
	token = get_internal_token()["access_token"]
	ensure_bucket()
	response = requests.get(
		f"https://developer.api.autodesk.com/oss/v2/buckets/{APS_BUCKET}/objects",
		headers={"Authorization":f"Bearer {token}"}
	)
	return response.json().get("items", [])

코드 설명

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 내부 토큰 발행, 데이터/버킷 관리 권한을 갖고 있는 보안 토큰
def get_internal_token():
	response = requests.post(
		# 토큰을 발행받는 엔드포인트
		"https://developer.api.autodesk.com/authentication/v2/token",
		# 토큰 발행용 CLIENT_ID, CLIENT_SECRET
		headers = {"Authorization" : f"Basic {authorization}"},
		data = {
			# 2legged 방식
			"grant_type" : "client_credentials",
			# 데이터 접근 권한 설정
			"scope" : "data:read data:write data:create bucket:read bucket:create"
		}
	)
	# token 발급(json 형태로 발급됨)
	return response.json()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 파일 저장용 버킷 생성
def ensure_bucket():
	# 버킷 생성을 위한 토큰 가져오기
	token = get_internal_token()["access_token"]
	response = requests.post(
		# 버킷 생성용 엔드포인트
		"https://developer.api.autodesk.com/oss/v2/buckets",
		# 버킷 발행용 CLIENT_ID, CLIENT_SECRET
		headers = {"Authorization" : f"Bearer {token}"},
		# bucketKEY는 버킷 이름, policyKEY는 버킷의 영구성 지정 (persistent = 영구 버킷)
		json = {
			"bucketKey" : APS_BUCKET,
			"policyKey" : "persistent"
		}
	)
	# 버킷 생성 상태 코드 200 = 생성됨, 409 = 이미 있음
	return response.status_code in [200, 409]
1
2
3
4
5
6
7
8
9
10
11
# 버킷안의 파일 리스트 반환
def get_objects():
	token = get_internal_token()["access_token"]
	# 버킷 가져오기
	ensure_bucket()
	response = requests.get(
	f"https://developer.api.autodesk.com/oss/v2/buckets/{APS_BUCKET}/objects",
	headers={"Authorization":f"Bearer {token}"}
	)
	# 버킷의 파일 리스트 불러오기, 없으면 빈리스트 [] 표시
	return response.json().get("items", [])
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.

인기 태그