2013年12月19日木曜日

[Ruby]Zaim API V2に対応した認証用ライブラリを作ってみた

家計簿ソフトZaim(https://zaim.net)が提供するWebAPIの認証部分を
omniauthを参考に作ってみた。

ソースコードは以下のサイトにアップロード。
https://github.com/maar4569/omniauth-zaimv2

参考にしたのは以下のライブラリ。(v1用のライブラリ。)
mururuさんありがとうございます。

https://github.com/mururu/omniauth-zaim


以前Google Apps ScriptからZaim APIをたたこうとして失敗し涙したが、
今回は成功。

Railsに組み込む場合、コールバック用のコントローラに
以下のような記述をしておくと、verify APIのレスポンスが得られる。

#サンプル
def callback
  auth = request.env['omniauth.auth']
  @user_id = auth.info.id
  @name = auth.info.name
  @input_count = auth.info.input_count
  @day_count = auth.info.day_count
  @repeat_count = auth.info.repeat_count
  @day = auth.info.day
  @week = auth.info.week
  @currency_code = auth.info.currency_code"
end

また、カテゴリに関する情報を取得する場合は以下のコードで取得できる。
 api_url = auth.extra.access_token.consumer.options[:site] + "/v2/home/category"
 raw_info = MultiJson.load(auth.extra.access_token.get(api_url).body)['categories']
 p "categries =>  #{raw_info}"

ジャンルなども基本的には同じ要領で取得する。

Zaimの情報を分析して家計を助けるぞー

0 件のコメント:

コメントを投稿