From f86260d8365e523e40909cd7faf07012c10cfaea Mon Sep 17 00:00:00 2001 From: ota42y Date: Mon, 6 Jun 2016 13:27:14 +0900 Subject: [PATCH 1/4] add authorize --- lib/fitgem/client.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/fitgem/client.rb b/lib/fitgem/client.rb index 90cf028..1276895 100644 --- a/lib/fitgem/client.rb +++ b/lib/fitgem/client.rb @@ -119,6 +119,25 @@ def initialize(opts) @api_locale = opts[:locale] || Fitgem::ApiLocale.US end + # Get authorize_url + # + # @param [String] Space separated access scope (i.e. 'activity nutrition') + # @param [String] Redirect URI + # @return [String] Authorize URL + def authorize_url(scope, redirect_uri) + consumer.auth_code.authorize_url(redirect_uri: redirect_uri, scope: scope) + end + + # Get token + # + # @param [String] Authorization code + # @param [String] Redirect URI + # @return [OAuth2::AccessToken] Accesstoken and refresh token + def get_token(authorization_code, redirect_uri) + encode = Base64.encode64("#{@consumer_key}:#{@consumer_secret}") + consumer.auth_code.get_token(authorization_code, headers: {'Authorization' => "Basic #{encode}"}, redirect_uri: redirect_uri) + end + private def consumer From 6929131b58e2638b39653043ce50b1035b40347c Mon Sep 17 00:00:00 2001 From: ota42y Date: Mon, 6 Jun 2016 19:44:28 +0900 Subject: [PATCH 2/4] add auth test --- lib/fitgem/client.rb | 2 +- spec/fitgem_client_spec.rb | 29 +++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/fitgem/client.rb b/lib/fitgem/client.rb index 1276895..5a9bbef 100644 --- a/lib/fitgem/client.rb +++ b/lib/fitgem/client.rb @@ -135,7 +135,7 @@ def authorize_url(scope, redirect_uri) # @return [OAuth2::AccessToken] Accesstoken and refresh token def get_token(authorization_code, redirect_uri) encode = Base64.encode64("#{@consumer_key}:#{@consumer_secret}") - consumer.auth_code.get_token(authorization_code, headers: {'Authorization' => "Basic #{encode}"}, redirect_uri: redirect_uri) + consumer.auth_code.get_token(authorization_code, headers: {Authorization: "Basic #{encode}"}, redirect_uri: redirect_uri) end private diff --git a/spec/fitgem_client_spec.rb b/spec/fitgem_client_spec.rb index eb1db5d..8c45930 100644 --- a/spec/fitgem_client_spec.rb +++ b/spec/fitgem_client_spec.rb @@ -4,12 +4,24 @@ let(:access_token) { double 'Access Token', :get => response, :request => response } + let(:consumer_key) { '12345' } + let(:consumer_secret) { '67890' } let(:client) { Fitgem::Client.new({ - :consumer_key => '12345', - :consumer_secret => '67890' + :consumer_key => consumer_key, + :consumer_secret => consumer_secret }) } let(:response) { double :body => {:foo => :bar}.to_json, :status => 200 } let(:consumer) { double 'Consumer' } + let(:redirect_uri) { 'http://example.com/redirect_url' } + let(:auth_url) { 'http://example.com/auth_url' } + let(:auth_code) { 'code' } + let(:scope) { 'activity nutrition heartrate location nutrition profile settings sleep social weight' } + let(:auth_header) { + { + Authorization: "Basic #{Base64.encode64("#{consumer_key}:#{consumer_secret}")}" + } + } + let(:auth_token) { 'token' } before :each do allow(OAuth2::Client).to receive(:new).with('12345', '67890', { @@ -18,6 +30,19 @@ :authorize_url => "https://www.fitbit.com/oauth2/authorize" }).and_return(consumer) allow(OAuth2::AccessToken).to receive(:new).and_return(access_token) + + allow(consumer).to receive_message_chain('auth_code.authorize_url'). + with({scope: scope, redirect_uri: redirect_uri}).and_return(auth_url) + allow(consumer).to receive_message_chain('auth_code.get_token'). + with(auth_code, {headers: auth_header, redirect_uri: redirect_uri}).and_return(auth_token) + end + + it 'get authorize url' do + expect(client.authorize_url(scope, redirect_uri)).to eq auth_url + end + + it 'get token'do + expect(client.get_token(auth_code, redirect_uri)).to eq auth_token end it 'returns JSON from the request' do From da7d183086738407a4fad8718339a8260c51321c Mon Sep 17 00:00:00 2001 From: ota42y Date: Tue, 14 Jun 2016 13:33:13 +0900 Subject: [PATCH 3/4] add refresh token method --- lib/fitgem/client.rb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/fitgem/client.rb b/lib/fitgem/client.rb index 5a9bbef..48c2dbc 100644 --- a/lib/fitgem/client.rb +++ b/lib/fitgem/client.rb @@ -134,8 +134,19 @@ def authorize_url(scope, redirect_uri) # @param [String] Redirect URI # @return [OAuth2::AccessToken] Accesstoken and refresh token def get_token(authorization_code, redirect_uri) - encode = Base64.encode64("#{@consumer_key}:#{@consumer_secret}") - consumer.auth_code.get_token(authorization_code, headers: {Authorization: "Basic #{encode}"}, redirect_uri: redirect_uri) + consumer.auth_code.get_token(authorization_code, headers: auth_header, redirect_uri: redirect_uri) + end + + # Refresh access token + # + # @param [String] Refresh token + # @return [OAuth2::AccessToken] Accesstoken and refresh token + def refresh_access_token!(refresh_token) + new_access_token = OAuth2::AccessToken.new(consumer, @token, refresh_token: refresh_token) + new_access_token.refresh!(headers: auth_header) + @token = new_access_token.token + @access_token = nil + access_token end private @@ -201,5 +212,9 @@ def default_headers 'Accept-Locale' => @api_locale } end + + def auth_header + {Authorization: "Basic #{ Base64.encode64("#{ @consumer_key }:#{ @consumer_secret }") }" } + end end end From 3cbc2c9a7bce12edb99f4b7b5307d660f5eb5d04 Mon Sep 17 00:00:00 2001 From: ota42y Date: Mon, 11 Jul 2016 20:16:32 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=90=9B=20=20fix=20refresh=20access=20?= =?UTF-8?q?token=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/fitgem/client.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/fitgem/client.rb b/lib/fitgem/client.rb index 48c2dbc..3b0e9a3 100644 --- a/lib/fitgem/client.rb +++ b/lib/fitgem/client.rb @@ -143,10 +143,11 @@ def get_token(authorization_code, redirect_uri) # @return [OAuth2::AccessToken] Accesstoken and refresh token def refresh_access_token!(refresh_token) new_access_token = OAuth2::AccessToken.new(consumer, @token, refresh_token: refresh_token) - new_access_token.refresh!(headers: auth_header) - @token = new_access_token.token + # refresh! method return new object not itself and not change itself + new_token = new_access_token.refresh!(headers: auth_header) + @token = new_token.token @access_token = nil - access_token + new_token end private