네이버 스마트스토어

STEP#3 - 네이버 커머스API 인증 토큰 발급 요청

nernado 2026. 1. 13. 22:14

이제 코딩 시작이다.

커머스API 개발 코딩에서 첫 번째로 해야할 일이다.

 

각종 API를 호출하기 위해서는 토큰이 필요하다.

해당 토큰값을 얻는 과정이다.

 

내 스마트스토어에 대한 데이터 조회 및 변경을 해야하는데,

내 스마트스토어를 변경할 수 있는 권한을 획득하는 것이다.

권한 설정을 위해 인증센터에서 Application 등록을 했고,

"애플리케이션ID" 와 "애플리케이션 시크릿"을 가지고 있을 것이다.

 

해당 정보를 바탕으로 권한을 획득하면 된다.

# API연동을 위한 준비
        def get_token(client_id, client_secret, type_="SELF") -> str:
            timestamp = str(int((time.time()-3) * 1000))
            pwd = f'{client_id}_{timestamp}'
            hashed = bcrypt.hashpw(pwd.encode('utf-8'), client_secret.encode('utf-8'))
            client_secret_sign = pybase64.standard_b64encode(hashed).decode('utf-8')

            headers = {"content-type": "application/x-www-form-urlencoded"}
            data_ = {
                "client_id": client_id,
                "timestamp": timestamp,
                "client_secret_sign": client_secret_sign,
                "grant_type": "client_credentials",
                "type": type_
            }

            query = urllib.parse.urlencode(data_)
            url = 'https://api.commerce.naver.com/external/v1/oauth2/token?' + query
            res = requests.post(url=url, headers=headers)
            res_data = res.json()

            while True:
                if 'access_token' in res_data:
                    token = res_data['access_token']
                    return token
                else:
                    print(f'[{res_data}] 토큰 요청 실패')
                    time.sleep(1)

 

인증 토큰 발급 요청 - 커머스API 공식 문서

https://apicenter.commerce.naver.com/docs/commerce-api/current/exchange-sellers-auth

 

인증 토큰 발급 요청 | 커머스API

API 활용을 위한 인증 토큰을 발급/갱신 요청합니다.<br />동일 리소스(type과 account_id에 따라)에는 하나의 인증 토큰의 발급되며 인증 토큰의 유효 시간은 3시간(10,800초)입니다. 발급된 인증 토큰이

apicenter.commerce.naver.com

 

토큰의 유효시간은 10,800초 (3시간) 이다.

이정도 시간이면 한 프로그램 사이클 안에서 사용하기 충분할거다.

최초 프로그램 실행 시, token을 저장하고 API 호출시 활용하도록 logic을 구성하면 된다.

 

이미 발급된 token이 있다면,

신규로 토큰이 발급되지 않고 기존 token값을 받게 된다

 * 토큰 유효시간이 30분 미만일 경우, 새로운 인증 token이 발급 됨.

 

해당 토큰값을 main 함수 또는 전역변수에 적절히 저장하여 잘 활용해 보자.

 

다음에는 발급된 token 값을 활용하여

다양한 API 호출하는 예시들을 알아보도록 하겠다.