본문 바로가기
Python/LOSTARK API

로스트아크 API - API 키 발급 및 Python 코드로 기본 응답 확인하기

by 깐테 2023. 4. 11.

Lostark OpenAPI Developer Portal

 

Lostark OpenAPI Developer Portal

Open API For All Developers START BUILDING YOUR OWN CLIENTS TODAY USING OFFICIAL DATA. GET ACCESS TO LOSTARK API

developer-lostark.game.onstove.com

 

  1. 먼저 위 사이트로 이동하여 Stove 로그인 후, API KEY를 받아준다.
  2. GET ACCESS TO LOSTARK API 클릭

3. CREATE A NEW CLIENT → CLIENT NAME은 필수로 적고 모든 동의 사항에 체크 후 CREATE

 

4. 해당 API KEY는 복사해둔다.

 

Lostark OpenAPI Developer Portal

위 사이트로 이동하면 기본 예제들을 확인 할 수 있다.

 

Lostark OpenAPI Developer Portal

USAGE GUIDE This guide describes how to interact with Lostark Open APIs. This is a technical guide intended for software developers, which walks you through implementation details and sample codes. Basic HTTP Making a valid HTTP request to Lostark Open API

developer-lostark.game.onstove.com

 


Response 확인

Javascript example

// Before run this code, need to install xhr2
// command: npm install xhr2
var XMLHttpRequest = require('xhr2');
var xhr2 = new XMLHttpRequest();
xhr2.open("GET", "https://developer-lostark.game.onstove.com/exmaple/api", true);
xhr2.setRequestHeader('accept', 'application/json');
xhr2.setRequestHeader('authorization', 'bearer your_token');
xhr2.onreadystatechange = () => { };
xhr2.onload = () => { console.log(xhr2.response); };
xhr2.send();
  • example 페이지에 있는 예제를 그대로 copy하여 실행하면 Node.js 환경에서 실행되지 않음
  • VS Code를 사용하는 경우라면 코드를 위와 같이 활용하면 콘솔에 출력할 수 있다.

 

Python example

# lostark_api_token.py
# 복사한 토큰을 아래와 같이 입력
# bearer 문구를 지우게 되면 에러코드가 발생하므로 지우지 않도록 한다.
Token = 'bearer your_JWT'
import requests
import json
from lostark_api_token import Token

headers = {
    'accept' : 'application/json',
    'authorization' : Token
}

url = 'https://developer-lostark.game.onstove.com/example/api'
response = requests.get(url, headers=headers)

print(response)
  • lostark_api_token.py 라는 이름으로 파일을 하나 만들고, 거기에 토큰값을 저장해서 사용함.
  • bearer 문구 뒤에 토큰을 붙여넣고, bearer를 지우지 않도록 한다.

 

Lostark OpenAPI Developer Portal

 

Lostark OpenAPI Developer Portal

USAGE GUIDE This guide describes how to interact with Lostark Open APIs. This is a technical guide intended for software developers, which walks you through implementation details and sample codes. Basic HTTP Making a valid HTTP request to Lostark Open API

developer-lostark.game.onstove.com

Lostark OpenAPI Developer Portal

 

Lostark OpenAPI Developer Portal

GETTING STARTED You can get started using Lostark Open API following basic procedures below and understand how it works. Login Before you hit any of Lostark Open APIs, you need to sign in to your Stove.com account. If you don't have one yet, please sign up

developer-lostark.game.onstove.com

  • 위 페이지에서 응답 코드 확인 가능. 200이면 OK, 401이면 Unauthorized 상태이므로 입력한 토큰이 잘못되지는 않았는지, bearer 라는 문구를 삭제하지는 않았는지 확인

 

  • response code가 위와 같으면 OK

 


파이썬 예제

  • API 가이드 페이지에는 curl, js에 대한 예제만 존재하지만 request 라이브러리가 파이썬에도 있기 때문에 파이썬에서도 실행 가능

 

Lostark OpenAPI Developer Portal

 

Lostark OpenAPI Developer Portal

USAGE GUIDE This guide describes how to interact with Lostark Open APIs. This is a technical guide intended for software developers, which walks you through implementation details and sample codes. Basic HTTP Making a valid HTTP request to Lostark Open API

developer-lostark.game.onstove.com

  • 호출 가능한 API는 위 페이지의 왼쪽 탭에서 확인 가능 뉴스, 캐릭터, 장비 정보, 경매장, 길드, 거래소, 게임 내 컨텐츠 정보 등을 호출 가능

 

API를 이용하여 JSON 형식으로 호출하기

import requests
import json
from lostark_api_token import Token

headers = {
    'accept' : 'application/json',
    'authorization' : Token
}

url = 'https://developer-lostark.game.onstove.com/news/events'

response = requests.get(url, headers=headers)
jsonObject = response.json()

print(response)
print(jsonObject)

 

  • response는 응답 코드를 출력하고 jsonObject는 json 형태의 데이터를 받아와서 출력한다.

 

 

JSON 형태의 데이터에서 특정 값만 불러오기

import requests
import json
from lostark_api_token import Token

headers = {
    'accept' : 'application/json',
    'authorization' : Token
}

url = 'https://developer-lostark.game.onstove.com/news/events'

response = requests.get(url, headers=headers)
jsonObject = response.json()

print(response)

# json 출력 코드
for list in jsonObject :
    print(list.get("Title"))

  • 다음과 같이 값을 가져올 수 있다
  • print(list.get(”Title”) 코드로 json 형태의 값에서 제목에 해당하는 값만 가져와서 출력

 

List로 가져올 때

import requests
import json
from lostark_api_token import Token

headers = {
    'accept' : 'application/json',
    'authorization' : Token
}

url = 'https://developer-lostark.game.onstove.com/news/events'

response = requests.get(url, headers=headers)
jsonObject = response.json()

print(response)
# print(jsonObject)

# cnt = 0
# json 출력 코드
# for list in jsonObject :
#     print(f"{cnt}번: ",list.get("Title"))
#     cnt += 1

# list로 출력 코드
jl = list()
for title in jsonObject :
    jl.append(title.get("Title"))

for i in range(len(jl)):
    print(f"{i}번: ",jl[i])

 

** 참고

 

Convert curl commands to code

GitHub is matching all contributions to this project on GitHub Sponsors. Contribute Now

curlconverter.com

 

반응형