From e27f4db7c40d6d543545a4ee540bc5e9ea0427df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Noce?= Date: Fri, 15 Sep 2017 15:00:04 +0200 Subject: [PATCH] Add support for the Mobile SDK Adds `is_mobile_sdk` option that changes how the token is handled to the sdk, to allow authentication using the tokens retreived with the mobile SDK. --- lib/linked_in/api.rb | 19 +++++++++++++++---- spec/linked_in/api/api_spec.rb | 9 +++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/linked_in/api.rb b/lib/linked_in/api.rb index db90c60..85b9b69 100644 --- a/lib/linked_in/api.rb +++ b/lib/linked_in/api.rb @@ -1,12 +1,13 @@ module LinkedIn class API - attr_accessor :access_token + attr_accessor :access_token, :options - def initialize(access_token=nil) + def initialize(access_token=nil, options={}) access_token = parse_access_token(access_token) verify_access_token!(access_token) @access_token = access_token + @options = options @connection = LinkedIn::Connection.new params: default_params, headers: default_headers @@ -60,6 +61,10 @@ def initialize(access_token=nil) :update_comment, :network_updates + def mobile_sdk? + !!(options[:is_mobile_sdk] || options["is_mobile_sdk"]) + end + private ############################################################## def initialize_endpoints @@ -74,12 +79,18 @@ def initialize_endpoints def default_params # https//developer.linkedin.com/documents/authentication - return {oauth2_access_token: @access_token.token} + mobile_sdk? ? {} : {oauth2_access_token: @access_token.token} end def default_headers # https://developer.linkedin.com/documents/api-requests-json - return {"x-li-format" => "json"} + mobile_sdk? ? + { + "x-li-format" => "json", + "x-li-src" => "msdk", + "Authorization": "Bearer #{@access_token.token}" + } : + {"x-li-format" => "json"} end def verify_access_token!(access_token) diff --git a/spec/linked_in/api/api_spec.rb b/spec/linked_in/api/api_spec.rb index 24dc321..2452c5a 100644 --- a/spec/linked_in/api/api_spec.rb +++ b/spec/linked_in/api/api_spec.rb @@ -38,4 +38,13 @@ include_examples "test access token" end + + context "With a string access token and `is_mobil_sdk` turned on" do + let(:access_token) { "dummy_access_token" } + let(:options) {{ is_mobile_sdk: true }} + + subject {LinkedIn::API.new(access_token, options)} + + include_examples "test access token" + end end