lib/ringcentral_sdk/platform/platform.rb in ringcentral_sdk-0.1.3 vs lib/ringcentral_sdk/platform/platform.rb in ringcentral_sdk-0.1.4
- old
+ new
@@ -17,38 +17,45 @@
API_VERSION = 'v1.0'
URL_PREFIX = '/restapi'
attr_accessor :server_url
- attr_reader :client
+ attr_reader :client
+ attr_reader :token
def initialize(app_key='', app_secret='', server_url=RingCentralSdk::Sdk::RC_SERVER_SANDBOX)
-
@app_key = app_key
@app_secret = app_secret
@server_url = server_url
@token = nil
@client = nil
-
end
def get_api_version_url()
return @server_url + URL_PREFIX + '/' + API_VERSION
end
def authorize(username='', extension='', password='', remember=false)
+ oauth2client = get_oauth2_client()
- oauth2 = OAuth2::Client.new(@app_key, @app_secret,
- :site => @server_url,
- :token_url => TOKEN_ENDPOINT)
-
- token = oauth2.password.get_token(username, password, {
+ token = oauth2client.password.get_token(username, password, {
:extension => extension,
:headers => { 'Authorization' => 'Basic ' + get_api_key() } })
authorized(token)
+ end
+ def set_token(token=nil)
+ if token.is_a?(OAuth2::AccessToken)
+ authorized(token)
+ elsif token.is_a?(Hash)
+ oauth2client = get_oauth2_client()
+ oauth2token = OAuth2::AccessToken::from_hash(oauth2client, token)
+ authorized(oauth2token)
+ else
+ raise "Invalid Token"
+ end
end
def authorized(token=nil)
@token = token
@@ -57,10 +64,15 @@
conn.request :json
conn.request :url_encoded
conn.response :json, :content_type => 'application/json'
conn.adapter Faraday.default_adapter
end
+ end
+ def get_oauth2_client()
+ return OAuth2::Client.new(@app_key, @app_secret,
+ :site => @server_url,
+ :token_url => TOKEN_ENDPOINT)
end
def get_api_key()
api_key = (@app_key.is_a?(String) && @app_secret.is_a?(String)) \
? Base64.encode64("#{@app_key}:#{@app_secret}").gsub(/[\s\t\r\n]/,'') : ''