일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- heroku
- Firefox
- terraform
- 프로그래머스 코딩테스트 연습문제
- PostgreSQL
- c#
- 프로그래머스 월간 코드 챌린지 시즌1
- 스코페2021
- Python
- Spring Boot
- 프로그래머스 코딩 테스트 연습
- selenium
- git
- 프로그래머스 월간 코드 챌린지
- Codeforces
- PostgreSQL 설치 시 에러
- github actions
- 파이썬
- 바이오데이터 엔지니어
- github
- WPF
- 스프링 부트와 AWS로 혼자 구현하는 웹 서비스
- 브랜디
- FastAPI
- pycharm
- 디자인 패턴
- Word Cloud
- 프로그래머스 코딩테스트 연습
- 클린 코드
- 애드센스
Archives
- Today
- Total
프로그래밍 연습하기
C# 업비트 API 활용해보기 본문
반응형
업비트에서는 다양한 API를 제공해서 내 계좌 조회, 코인의 현재 시세 등 다양한 정보를 활용할 수 있습니다.
그런데 C# 예제는 없어서 한번 간단하게 작성해봤습니다.
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using JWT;
using JWT.Algorithms;
using JWT.Serializers;
namespace upbit_console
{
class Program
{
static string ticker_url = "https://api.upbit.com/v1/ticker";
static string account_url = "https://api.upbit.com/v1/accounts";
static string UUID = Guid.NewGuid().ToString();
static string AccessKey = ""; //발급받은 AccessKey를 넣어줍니다.
static string SecretKey = ""; //발급받은 SecretKey를 넣어줍니다.
static string AccountInquiry() //나의 계좌 조회 (전체 계좌 조회)
{
var payload = new Dictionary<string, object>
{
{ "access_key" , AccessKey },
{ "nonce" , UUID },
};
IJwtAlgorithm algorithm = new HMACSHA256Algorithm(); //JWT 라이브러리 이용하여 JWT 토큰을 만듭니다.
IJsonSerializer serializer = new JsonNetSerializer();
IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder);
var token = encoder.Encode(payload, SecretKey);
var authorize_token = string.Format("Bearer {0}", token);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(account_url); //요청 과정
request.Method = "GET";
request.Headers.Add(string.Format("Authorization:{0}", authorize_token));
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string strResult = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
return strResult;
}
static string CoinInquiry(string coinName) //특정 코인의 현재 시세 조회 (현재가 정보)
{
StringBuilder dataParams = new StringBuilder();//요청 과정
dataParams.Append("markets="+coinName);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ticker_url + "?" + dataParams);
request.Method = "GET";
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string strResult = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
return strResult;
}
static void Main(string[] args)
{
string strResult1 = CoinInquiry("KRW-BTC");
string strResult2 = AccountInquiry();
Console.WriteLine(strResult1+"\n");
Console.WriteLine(strResult2);
Console.ReadLine();
}
}
}
기본적으로 AccessKey, SecretKey에 자신이 발급받은 키를 넣어줘야 합니다.
함수 AccountInquiry는 자신의 계좌 조회, CoinInquiry는 특정 코인의 현재 시세를 조회 후 String을 반환합니다.
자세한 내용은 업비트 API 문서를 참고해주세요.
또한 JWT 라이브러리를 설치해야합니다.
https://www.nuget.org/packages/JWT
비주얼 스튜디오 2019 기준으로 도구->NuGet 패키지 관리자->패키지 관리자 콘솔 실행 후
Install-Package JWT -Version 5.2.2
를 입력하시면 설치됩니다.
패키지 의존성과 프로젝트 대상 프레임워크 등을 잘 확인하세요.
반응형
'C#' 카테고리의 다른 글
C# WPF 창의 활성화 이벤트를 사용하는 법 (0) | 2020.08.01 |
---|---|
C# WPF 다른 창에 있는 정보 가져오기 (0) | 2020.07.31 |
C#에서 현재 경로 알아내기, 디렉토리 생성하기 (0) | 2020.07.31 |
C# WPF 새로운 창(Window) 만들기 (0) | 2020.07.31 |
C# WPF로 일기장 만들기 (0) | 2020.07.31 |
Comments