티스토리 뷰

은행권 공동 오픈 플랫폼 api 가져와 레일스로 만든프로젝트에 출력해보기!


말은 거창하지만 별거없슴다 하하



먼저 실습을 하기에 앞서, 간단히 숙지해야 할 내용을 정리하겠습니당.



1. API 호출


api 를 사용할때, 발급받은 액세스 토큰으로 API에 요청할 수 있다.

이를 전달 토큰(bearer token)이라 하고, 주로 헤더에 넣어 보낸다.

우리는 미리 발급된 액세스 토큰을 사용할 예정이다.


"Authorization: Bearer 액세스 토큰"



2. Postman 


get 방식이나, post 방식으로 데이터를 요청하거나 전달 받을때 사용하는 프로그램이다!


https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop


위다운로드 주소를 참고하기 바랍니당




실습하기


https://developers.open-platform.or.kr/guide/start 

  1. 사이트에 접속한다. (테스트 api를 사용해 은행권 공동플랫폼 데이터를 요청할 수 있는사이트이다.

  2. 사이트 하단으로 이동하면 아래와 같은 테스트에 필요한 정보들이 나와있다. access-token 이라는 부분에 있는 값들을 우리가 api요청을 할때, 헤더에 포함시켜, 원하는 정보를 얻을 권한을 가지게 된다.


3.사이트 맨아래쪽에 API콘솔 경험하기를 클릭하게 되면 api 를 어떻게 요청하는지에 대한 url 명세가 설명되어있다. 우리는 이번에 사용자의 계좌를 조회 할것이니, 사용자부분 (/user)을 살펴본다. 이 url을 선택한 이유는 넘겨야하는 parameter가 하나만 있으면 되서 비교적 용이해 선택했다.

https://developers.open-platform.or.kr/guide/console#//user 
위의 주소는 api콘솔경험하기 주소입니당



  • 위에 대해 간단히 설명하자면, api요청을 하는 url이 /user/me와 같은 형태로 url을 요청할수 있고,

응답이 왔을때의 schema가 노란색부분에 나와있다.


  • 즉, 응답이 저런 형식으로 온다는것이다 크헤헿

response content type은 응답이 올때의 type을 뜻하는것이다.


  • parameter 부분은 요청을 보낼때 같이 보내는 정보이다. 즉 지금과 같은 경우는 사용자 일련번호를 같이 넘겨보내, 그에 해당하는 사용자의 정보를 조회하는것이다.
이제 postman을 켜보자.



get이라 써있는 곳 옆에 

https://testapi.open-platform.or.kr

api주소를 넣어준다.


그리고 /user/me라는 url에 정보를 요청해야하므로 /user/me 를 옆에 써준다.


https://testapi.open-platform.or.kr/user/me 라는 주소로 데이터를 요청하게 되는것이다.

  • 그리고 주소옆 params 버튼을 눌러 아래와 같이 parameter들을 넣어준다.



  • 마지막으로 Authorization 탭을 눌러 아래와 같이 Bearer Token  을 선택해 
  •  https://developers.open-platform.or.kr/guide/start 사이트 하단에 있었던 가상 사용자의 access token을 넣어준다. 사용자권한이 로그인일때 로그인 연결된 사용자 정보를 조회할수있으므로,  token에 해당하는 토큰인074c68d8-37be-4566-9c1a-ae06fff26ef3 를 넣어준다.



그리고 send를 해주면 아래와 같은 json 데이터가 응답되는것을 알수있다.!

{
   "api_tran_id": "aa397c49-938d-45ab-81bb-5ce5aaebab42",
   "rsp_code": "A0000",
   "rsp_message": "",
   "api_tran_dtm": "20180203211259806",
   "user_seq_no": "0100000001",
   "user_ci": "",
   "user_name": "홍길O",
   "res_cnt": "3",
   "res_list": [
      {
           "fintech_use_num": "101600000169321934066676",
           "account_alias": "오픈은행 출금용",
           "bank_code_std": "097",
           "bank_code_sub": "0971234",
           "bank_name": "오픈은행",
           "account_num_masked": "333444555***",
           "account_holder_name": "홍길동",
           "account_type": "P",
           "inquiry_agree_yn": "",
           "inquiry_agree_dtime": "",
           "transfer_agree_yn": "Y",
           "transfer_agree_dtime": "20160824160318"
      },
      {
           "fintech_use_num": "101600000169321934066595",
           "account_alias": "오픈은행 특별계좌",
           "bank_code_std": "097",
           "bank_code_sub": "0971234",
           "bank_name": "오픈은행",
           "account_num_masked": "222333444***",
           "account_holder_name": "홍길동",
           "account_type": "P",
           "inquiry_agree_yn": "Y",
           "inquiry_agree_dtime": "20160824155755",
           "transfer_agree_yn": "",
           "transfer_agree_dtime": ""
      },
      {
           "fintech_use_num": "101600000169321934052424",
           "account_alias": "계좌별명테스트",
           "bank_code_std": "097",
           "bank_code_sub": "0971234",
           "bank_name": "오픈은행",
           "account_num_masked": "1122334***",
           "account_holder_name": "홍길동",
           "account_type": "P",
           "inquiry_agree_yn": "Y",
           "inquiry_agree_dtime": "20160822210337",
           "transfer_agree_yn": "Y",
           "transfer_agree_dtime": "20160822210500"
      }
  ]
}

성공적으로 user의 계좌데이터를 불러왔다.


이제 rails에서 이 데이터들을 활용해볼예정이다.


  • http requset를 하기위해 rest-clinet gem을 활용했다.

rails g scaffold Post name:string bank:string bankNum:integer
gem 'rest-client'
bundle install


require 'rest-client' posts_controller 맨위에 추가해주고
index 액션안에

@response=RestClient.get
'https://testapi.open-platform.or.kr/user/me?user_seq_no=0100000001',
{:Authorization => 'Bearer 074c68d8-37be-4566-9c1a-ae06fff26ef3'}

추가해준다.

  • 위와같이 rails g scaffold 블라블라 명령어를 통해 스캐폴딩을 해줍니다.
  • rest-clinet gem을 설치해줍니다. (http requset를 용이하게 해주는 gem입니다.)
  • bundle install를 해줍니당!
  • 그리고 post 컨트롤러안에 require 'rest-client'를 맨위에 적어주고, index 액션안에 @reponse   블라블라~~ 를 넣어줍니당. 붙여놓은 내용은, 앞서 포스트맨으로 살펴본 header에 토큰을 넣어주고, api를 요청하는 uri정보가 들어가있습니다.!!! 
  • 여기선 코드안에 토큰을 넣어줬는데, test하기위해 이미 발급된 토큰을 활용해서 문제가 되지않지만, 실제 자신의 토큰을 발급받아 사용할땐 위와같이 넣어주면 보안에 안좋을수 있습니당..ㅎ

@results = JSON.parse(@response, object_class: OpenStruct)
  • 마지막으로 @results안에 json 파싱을 해줘서 객체안에 담아줍니다. 뷰에서 용이하게 뿌려주기위해 위와 같은 과정을 거칩니당!



<h1>은행권공동 플랫폼 api를 가져와 계좌정보 출력하기</h1>

<table>
 <thead>
   <tr>
     <th>이름: <%=@results.user_name%></th>
     <th>계좌갯수: <%=@results.res_cnt%></th>
   </tr>
   <tr>
     <th>Bank</th>
     <th>Banknum</th>
     <th colspan="3"></th>
  </tr>
 </thead>

 <tbody>
   <% @results.res_list.each do |bank| %>
    <tr>
       <td><%= bank.bank_name %></td>
       <td><%= bank.account_num_masked %></td>
     </tr>
   <% end %>
</tbody>
</table>

위내용을 뷰파일의 index.html.erb에 넣어줍니다.!

{
   "api_tran_id": "aa397c49-938d-45ab-81bb-5ce5aaebab42",
   "rsp_code": "A0000",
   "rsp_message": "",
   "api_tran_dtm": "20180203211259806",
   "user_seq_no": "0100000001",
   "user_ci": "",
   "user_name": "홍길O",
   "res_cnt": "3",
   "res_list": [
      {
           "fintech_use_num": "101600000169321934066676",
           "account_alias": "오픈은행 출금용",
           "bank_code_std": "097",
           "bank_code_sub": "0971234",
           "bank_name": "오픈은행",
           "account_num_masked": "333444555***",
           "account_holder_name": "홍길동",
           "account_type": "P",
           "inquiry_agree_yn": "",
           "inquiry_agree_dtime": "",
           "transfer_agree_yn": "Y",
           "transfer_agree_dtime": "20160824160318"
      },
      {
           "fintech_use_num": "101600000169321934066595",
           "account_alias": "오픈은행 특별계좌",
           "bank_code_std": "097",
           "bank_code_sub": "0971234",
           "bank_name": "오픈은행",
           "account_num_masked": "222333444***",
           "account_holder_name": "홍길동",
           "account_type": "P",
           "inquiry_agree_yn": "Y",
           "inquiry_agree_dtime": "20160824155755",
           "transfer_agree_yn": "",
           "transfer_agree_dtime": ""
      },
      {
           "fintech_use_num": "101600000169321934052424",
           "account_alias": "계좌별명테스트",
           "bank_code_std": "097",
           "bank_code_sub": "0971234",
           "bank_name": "오픈은행",
           "account_num_masked": "1122334***",
           "account_holder_name": "홍길동",
           "account_type": "P",
           "inquiry_agree_yn": "Y",
           "inquiry_agree_dtime": "20160822210337",
           "transfer_agree_yn": "Y",
           "transfer_agree_dtime": "20160822210500"
      }
  ]
}

앞에서 postman으로 확인했던 json데이터와 비교해보면,

 index뷰안에서 @results.user_name, @results.res_cnt, @results.res_list와 같이 접근할수 있었던 이유를 알수있을겁니닿 하하


그리고 서버를 돌려주면



위와 같이 외부 api에서 불러온 json 데이터를 위와 같이 출력해줄수 있답니당!


데이터를 보여주기만하면되니 생각해보니 scaffolding을 해줄 필요가 없었군요 하하 아무튼 이상입니당



깃헙주소 :https://github.com/zx6658/bankPublic_api_rails

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
TAG
more
«   2024/05   »
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
글 보관함