Skip to content

OAuth2 Only Authentication

wlmcewen edited this page Nov 7, 2012 · 2 revisions

One authentication method provided by the client is the vanilla 'OAuth2' approach. This page goes over OAuth2 Authentication:

Example

# initialize the gem with your OAuth 2 key/secret.
# See also: script/oauth2_example.rb
# api_key, api_secret, and callback are all required.
# The following options are required:
#  - api_key: Your OAuth 2 client key
#  - api_secret: Your OAuth 2 client secret
#  - callback: Your OAuth 2 redirect_uri, which the end user will be redirected
#              to after authorizing your application to access their data.
#  - auth_endpoint: The URI to redirect the user's web browser to, in order for them to
#                   authorize your application to access their data.
# other options and their defaults:
#  - endpoint:   'https://api.sparkapi.com'
#  - version:    'v1'
#  - ssl:        true
#  - user_agent: 'Spark API Ruby Gem'
SparkApi.configure do |config|
  config.authentication_mode = SparkApi::Authentication::OAuth2
  config.api_key      = "YOUR_CLIENT_ID"
  config.api_secret   = "YOUR_CLIENT_SECRET"
  config.callback     = "YOUR_REDIRECT_URI"
  config.auth_endpoint = "https://sparkplatform.com/oauth2"
  config.endpoint   = 'https://sparkapi.com'
end

# Code is retrieved from the method. SparkApi.client.authenticator.authorization_url
# See script/oauth2_example.rb for more details.


SparkApi.client.oauth2_provider.code = "CODE_FROM_ABOVE_URI"
SparkApi.client.authenticate

# Alternatively, if you've already received an access token, you may
# do the following instead of the above two lines:
#SparkApi.client.session = SparkApi::Authentication::OAuthSession.new "access_token"=> "ACCESS_TOKEN", 
#                           "refresh_token" => "REFRESH_TOKEN", "expires_in" => 86400

SparkApi.client.get "/system"

Also checkout the example script at "script/oauth2_example.rb".

Spark Platform Documentation

OAuth 2 Only Protocol