반응형
갑자기 비트코인 자동매매 프로그램이 만들고 싶어졌다.
일단 처음은 업비트 API 에서 Access Key와 Secret Key를 발급받고,
계좌 정보 가져오는 것부터 시작
1. 업비트 API에서 Access Key 발급
- 프로그램 만들면서 수행할 것들과 IP 주소를 등록한다.
- ipconfig명령어로 확인할 수 있는 IP는 내부 IP이므로 사용할 수 없음
- 네이버에서 "내 IP주소 확인"에서 나오는 IP 주소를 입력하자.
- Access Key 발급 완료!
2. Spring 프로젝트에서 계좌 조회 하기
- 나는 Spring project로 진행
- 일단 Reponse를 받을 수 있는 dto를 만들자
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class MyBank {
private String currency;
private String balance;
private String locked;
private String avg_buy_price;
private boolean avg_buy_price_modified;
private String unit_currency;
private String korean_name;
}
- 계좌 조회 코드는 API 개발자 센터 참고
public class TradingController {
public static List<MyBank> accounts() {
String accessKey = ""; // access key 입력
String secretKey = ""; // secret key 입력
String serverUrl = "https://api.upbit.com";
Algorithm algorithm = Algorithm.HMAC256(secretKey);
String jwtToken = JWT.create()
.withClaim("access_key", accessKey)
.withClaim("nonce", UUID.randomUUID().toString())
.sign(algorithm);
String authenticationToken = "Bearer " + jwtToken;
try {
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(serverUrl + "/v1/accounts");
request.setHeader("Content-Type", "application/json");
request.addHeader("Authorization", authenticationToken);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
List<MyBank> myBankList = new Gson().fromJson(EntityUtils.toString(entity, "UTF-8"), new TypeToken<List<MyBank>>() {}.getType());
return myBankList;
} catch (IOException e) {
return null;
}
}
}
3. 작동 확인
- 계좌 조회에 대한 Test Code 작성
public class TradingProjectApplication {
public static void main(String[] args) throws IOException {
// ========================= 계좌 조회
List<MyBank> listMyBankVo = null;
try {
listMyBankVo = TradingController.accounts();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(new Gson().toJson(listMyBankVo));
}
- 내가 보유한 코인 정보가 다 나온다..
반응형
'프로젝트' 카테고리의 다른 글
[프로젝트 A] 자동매매 프로그램 만들기 (2. 화면 구축) (3) | 2025.04.28 |
---|