Ruby를 이용한 오픈API 서명 방법
본 문서는 Ruby를 통해 Daum 인증 API 사용 및 블로그 API 사용을 위해 작성되었습니다.
서명 값 만들기 #
1. 오픈API 호출 URL 구성 #
- apikey만 있는 경우
http://apis.daum.net/search/dsa?apikey=4125603e35da5d8820b07dc7b19050dbc838336b &q=worldcup&results=10&start=1&output=rss
- apid가 추가된 경우
http://apis.daum.net/search/dsa?apid=ba8c53ab7970189d93a7 &apikey=4125603e35da5d8820b07dc7b19050dbc838336b &q=worldcup&results=10&start=1&output=rss
2. ts, nonce 생성 및 오픈 API 호출 url 에 포함 #
- ts는 현재 타임스탬프
- nonce는 16bytes의 난수 문자열
http://apis.daum.net/search/dsa?apikey=4125603e35da5d8820b07dc7b19050dbc838336b &q=worldcup&results=10&start=1&output=rss &ts=20060911061201 &nonce=f91cbfbaa8477afd
3. 서명 #
- 2번의 URL에 HMACSHA1 서명 알고리즘을 수행합니다. ts 와 nonce 값이 항상 변하기 때문에 실제 아래 서명값은 항상 다른 값이 나오게 됩니다.
- HMACSHA1 서명 알고리즘은 RFC 2014 에 정의되어 있습니다.
서명 대상 데이터 := "http://apis.daum.net/search/dsa?apikey=4125603e35da5d8820b07dc7b19050dbc838336b &q=worldcup&results=10&start=1&output=rss&ts=20060911061201&nonce=f91cbfbaa8477afd" sig := hmacsha1( 서명 대상 데이터 ) with signature key 서명값 예 : b45de8a31aed6e58b831dd72087bb4ed71fcaf98
4. 서명 URL 생성 #
- 2번 URL에 "sigalg=hmacsha1&sig=서명값" 을 추가합니다.
http://apis.daum.net/search/dsa?apikey=4125603e35da5d8820b07dc7b19050dbc838336b &q=worldcup&results=10&start=1&output=rss &ts=20060911061201 &nonce=f91cbfbaa8477afd &sigalg=hmacsha1 &sig=b45de8a31aed6e58b831dd72087bb4ed71fcaf98
http://apis.daum.net/search/dsa?apid=ba8c53ab7970189d93a7 &apikey=4125603e35da5d8820b07dc7b19050dbc838336b &q=worldcup&results=10&start=1&output=rss &ts=20060911061020 &nonce=068e146a0e591cf3 &sigalg=hmacsha1 &sig=3bc3cfffdc67c3e080646ca53ac4d5ccb3b6bca3
5. 서명 API 호출 #
- 이 URL로 오픈API를 호출합니다.
Ruby 서명 생성 라이브러리 #
# 라이브러리
require 'xmlrpc/client'
require 'openssl'
require 'digest/sha1'
class DaumBlogApi
NONCE_CHARS = "0123456789abcdef"
attr_accessor :api_key
attr_accessor :sig_key
def initialize(api_key, sig_key)
@api_key = api_key
@sig_key = sig_key
end
def new_post(title, description, options = {})
server = get_server
tags = options[:tags] || ""
server.call("daum.blog.openapi.newPost.execute", title, description, tags)
end
def get_server
nonce = ""
16.times { nonce << NONCE_CHARS[rand(NONCE_CHARS.length)] }
ts = Time.now.strftime('%Y%m%d%H%M%S')
url = "http://apis.daum.net/blog/XMLRPC.do?apikey=#{@api_key}"
url << "&ts=#{ts}&nonce=#{nonce}"
key = @sig_key.unpack('a2' * 32).map{|x| x.hex}.pack('c' * 32)
sha1 = OpenSSL::Digest::Digest.new('sha1')
sig = OpenSSL::HMAC.hexdigest(sha1, key, url)
url << "&sigalg=hmacsha1&sig=#{sig}"
XMLRPC::Client.new2(url)
end
end
# 테스트 코드
require 'pp'
api = DaumBlogApi.new('인증키', '서명키')
pp api.new_post("제목", "본문", :tags = > "태그1, 태그2") # 블로그에 글을 작성합니다. 등록된 글 번호를 반환합니다.
# 호출 결과 $ ruby daum_blog_api.rb "http://blog.daum.net/likejazz" "11402607"
이제 서명된 오픈API를 이용해 블로그에 글을 작성하실 수 있습니다.
get_server 메소드를 이용해 라이브러리에 추가 메소드를 작성하면 서명된 상태로 다른 API를 호출하실수도 있습니다.
# 라이브러리에 추가
def check_blog
server = get_server
server.call("daum.blog.openapi.checkBlog.execute")
end
# 테스트 코드
require 'pp'
api = DaumBlogApi.new('인증키', '서명키')
pp api.check_blog # 블로그가 있는지 여부를 체크합니다. 있으면 블로그 주소, 없으면 ""을 반환합니다.
이외에도 블로그명 중복체크 daum.blog.openapi.checkBlogName.execute등 서명된 상태로 다양한 API를 이용하실 수 있습니다.
[
소스 파일 다운로드 ]
소스 파일 다운로드 ]


