Token authentication via Rails Faraday gem

Hi,

Supporting limited test project of pulling some information from Pipedrive to corp DW, and having some challenges on authentication using a Pipedrive token, and the Faraday gem. The ruby (Rails environment) code (just to test the concept) is:

def self.test_api_blah(dsid)

base_url = 'https://{company_name}.pipedrive.com/api/v1/'
path = some_url_string

conn = Faraday.new(url: base_url) do |faraday|
 faraday.headers['Authorization'] = my_token_string
 faraday.adapter Faraday.default_adapter
end

begin
  response = conn.get(path)
rescue Faraday::Error => e
  puts e.response[:status]
  puts e.response[:body]
end

puts response

end

The code works, creates/has a response, but the response notes an authorization issue:

status=401,
reason_phrase=“Unauthorized”,
response_body=
“{"success":false,"error":"unauthorized access","errorCode":401,"error_info":"Please check developers.pipedrive.com"}”

I believe have the correct token (as a string), but is is unclear to me whether the move to push the authentication into the header works. Is there a log to review, or do any of the rails developers have any pointers (on my code/implementation).

Tony

1 Like

Hi! I’m not familiar with Faraday specifics but looks like you might be missing the word Bearer before the token string in the header. See OAuth authorization

Can you check if Faraday support Bearer auth by some configuration tweaks or just concatenate/prepend “Bearer” before you token

1 Like

Unfortunately, same response. The Faraday gem (a wrapper for http requests) has the following as the request:
method=:get,
request_body=nil,
url=#<URI::HTTPS Log in>,
request_headers={“Authorization”=“Bearer v1u:(token hidden here)”, “User-Agent”=>“Faraday v1.10.4”}

with a partial response of:

status=401,
reason_phrase=“Unauthorized”,
response_body=
“{"success":false,"error":"unauthorized access","errorCode":401,"error_info":"Please check developers.pipedrive.com"}”

Does anyone know if there is any connection to the version of the API, and the text ‘v1u:’ in the call itself (in the request header after the ‘Bearer’ text)?

T

Hi Simm,
Thanks for that. I had removed that at one point based on other content indicating that token authorizations did not require it, but I see the section you pointed me towards and it is included. Appreciate the help.
Tony

The auth header looks correct. v1u is a part of the OAuth token string so it not related to Faraday nor has any special meaning.

Have you verified that the access token and company domain are valid? Access tokens are valid for a maximum of 60 minutes and the company domain matters (using a company domain not associated with the access token will cause the request to fail).

You can try simply by making a CURL request in the console, for example:

curl --location 'https://YOURCOMPANY.pipedrive.com/api/v1/persons/2' \
--header 'Authorization: Bearer YOUR_ACCESS_TOKEN'

Siim: Thanks. Had multiple errors/issues, including in part of what was discussed here. In the end, the gem usage (wrapper on the api call), modified the final result. I also had ‘v1’ in my url, whereas ‘v2’ was required for Persons object.

As a final follow on, using the Faraday gem for Rails, my still evolving method looks like this:

def self.api_persons(person_id, company_url)

base_url = company_url
path = 'persons/' + person_id

conn = Faraday.new(url: base_url) do |faraday|
  faraday.headers['x-api-token'] = '{API KEY HERE}'
  faraday.adapter Faraday.default_adapter
  faraday.response :json
end

begin
  response = conn.get(path)
  rescue Faraday::Error => e
    puts e.response[:status]
    puts e.response[:body]
end

response.env[:data]

end

1 Like