Module: Withings::Api::OAuthActions

Includes:
OAuthBase
Included in:
Withings::Api
Defined in:
lib/withings-api/oauth_actions.rb

Overview

Simple API to ease the OAuth setup steps for Withing API client apps.

Specifically, this class provides methods for OAuth access token creation.

  1. Request request tokens - via #create_request_token

  2. Redirect to authorization URL (this is handled outside of these methods, ie: by the webapp, etc.)

  3. Request access tokens (for permanent access to Withings content) - via #create_access_token

Constant Summary collapse

Defaults =
Withings::Api::Defaults

Instance Method Summary collapse

Instance Method Details

#create_access_token(request_token, *arguments) ⇒ Object

Issues the “access_token” oauth HTTP request to Withings



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/withings-api/oauth_actions.rb', line 65

def create_access_token(request_token, *arguments)
  _consumer, _request_token, _user_id = nil

  if request_token.instance_of?(RequestTokenResponse) && arguments.length == 1
    _consumer = request_token.oauth_consumer
    _request_token = request_token.oauth_request_token
    _user_id = arguments.shift
  elsif request_token.instance_of?(RequestToken) && arguments.length == 2
    request_token.instance_of?(ConsumerToken)
    _consumer = create_consumer(*arguments.shift.to_a)
    _request_token = OAuth::RequestToken.new(_consumer, *request_token.to_a)
    _user_id = arguments.shift
  else
    raise ArgumentError
  end

  oauth_access_token = _consumer.get_access_token(_request_token)

  # test for unauthorized token, since oauth + withings doesn't turn this into an explicit
  # error code / exception
  if oauth_access_token.params.key?(:"unauthorized token")
    raise StandardError, :"unauthorized token"
  end

  AccessTokenResponse.new oauth_access_token
end

#create_request_token(consumer_token, *arguments) ⇒ RequestTokenResponse

Issues the “request_token” oauth HTTP request to Withings.

For call details @ Withings, see www.withings.com/en/api/oauthguide#access

For details about registering your application with Withings, see www.withings.com/en/api/oauthguide#registration

Raises:

  • (Timeout::Error)

    on connection, or read timeout

  • (SystemCallError)

    on low level system call errors (connection timeout, connection refused)

  • (ProtocolError)

    for HTTP 5XX error response codes

  • (OAuth::Unauthorized)

    for HTTP 4XX error reponse codes

  • (StandardError)

    for everything else



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/withings-api/oauth_actions.rb', line 38

def create_request_token(consumer_token, *arguments)
  _consumer_token, _consumer_secret, _callback_url = nil

  if arguments.length == 1 && consumer_token.instance_of?(Withings::Api::ConsumerToken)
    _consumer_token, _consumer_secret = consumer_token.to_a
  elsif arguments.length == 2
    _consumer_token = consumer_token
    _consumer_secret = arguments.shift
  else
    raise(ArgumentError)
  end
  _callback_url = arguments.shift

  # TODO: warn if the callback URL isn't HTTPS
  consumer = create_consumer(_consumer_token, _consumer_secret)
  oauth_request_token = consumer.get_request_token({:oauth_callback => _callback_url})

  RequestTokenResponse.new oauth_request_token
end