自己解決
Oauth認証
routeに認証画面・トークン取得用のURLを設定し、Userコントローラーにメソッド定義
↓users_controller.rb
Ruby
1def oauth
2 client = OAuth2::Client.new(ENV["API_ID"], ENV["API_SECRET"], :site => 'https://www.healthplanet.jp', :authorize_url => ' https://www.healthplanet.jp/oauth/auth',
3 :token_url => 'https://www.healthplanet.jp/oauth/token')
4 redirect_uri = 'https://body-w.herokuapp.com/users/callback'
5 authorize_url = "https://www.healthplanet.jp/oauth/auth?client_id=#{ENV["API_ID"]}&redirect_uri=#{redirect_uri}&scope=innerscan&response_type=code"
6 redirect_to authorize_url
7end
8
9def callback
10 client = OAuth2::Client.new(ENV["API_ID"], ENV["API_SECRET"],
11 {
12 site: 'https://www.healthplanet.jp/',
13 token_url: 'oauth/token',
14 }
15 )
16 token = client.auth_code.get_token(
17 params[:code],
18 {:redirect_uri => 'https://body-w.herokuapp.com/users/callback',
19 :grant_type => "authorization_code"}
20 )
21
22 access_token = token.token #AccessTokenオブジェクトからaccess_tokenをとりだす
23 refresh_token = token.refresh_token #AccessTokenオブジェクトからrefresh_tokenをとりだす
24・
25・
26・
データ取得
保存しておいたアクセストークンを使って、AccessTokenオブジェクトを生成。getメソッドで取得
/lib/tasks/scheduler.reakeに記述
Ruby
1.
2.
3.
4access_token = OAuth2::AccessToken.new(client, decrypt_access_token)
5
6resource_data = access_token.get('https://www.healthplanet.jp/status/innerscan.json', :params => { 'access_token' => access_token.token, 'tag' => '6021', 'date' => '0', 'from' => from.strftime('%Y%m%d%H%M%S'), 'to' => to.strftime('%Y%m%d%H%M%S') })
7.
8.
9.
タスク実行
「rake "タスク名"」をHeroku Schedulerに登録
###参考ページ