Module: SageOne::OAuth

Included in:
Client
Defined in:
lib/sage_one/oauth.rb

Overview

This module helps with setting up the OAuth connection to SageOne. After the two step process you will have an access_token that you can store and use for making future API calls.

Instance Method Summary collapse

Instance Method Details

#authorize_url(callback_url) ⇒ String

Generates the OAuth URL for redirecting users to SageOne. You should ensure the your SageOne.client_id is configured before calling this method.

Examples:

SageOne.authorize_url('https://example.com/auth/sageone/callback')

Parameters:

  • callback_url (String)

    SageOne OAuth will pass an authorization code back to this URL.

Returns:

  • (String)

    The URL for you to redirect the user to SageOne.



21
22
23
24
25
26
27
28
# File 'lib/sage_one/oauth.rb', line 21

def authorize_url(callback_url)
  params = {
    client_id:      client_id,
    redirect_uri:   callback_url,
    response_type: 'code'
  }
  connection.build_url("/oauth/authorize/", params).to_s
end

#get_access_token(code, callback_url) ⇒ Hashie::Mash

Returns an access token for future authentication.

Examples:

# Assuming (Rails) your code is stored in params hash.
SageOne.get_access_token(params[:code], 'https://example.com/auth/sageone/callback')

Parameters:

  • code (String)

    The authorisation code SageOne sent to your callback_url.

  • callback_url (String)

    The callback URL you used to get the authorization code.

Returns:

  • (Hashie::Mash)

    Containing the access_token for you to store for making future API calls.



38
39
40
41
42
43
44
45
46
47
# File 'lib/sage_one/oauth.rb', line 38

def get_access_token(code, callback_url)
  params = {
    client_id:      client_id,
    client_secret:  client_secret,
    grant_type:     'authorization_code',
    code:           code,
    redirect_uri:   callback_url
  }
  post("/oauth/token/", params)
end