# OAuth2::Error HTTP Basic: Access denied

**URL:** <https://api-discuss.freeagent.com/t/oauth2-error-http-basic-access-denied/3139>\
**Category:** Authentication\
**Created:** [13 January 2023 17:47 UTC](https://api-discuss.freeagent.com/t/oauth2-error-http-basic-access-denied/3139 "2023-01-13T17:47:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheRealNeil](https://api-discuss.freeagent.com/user_avatar/api-discuss.freeagent.com/therealneil/32/575_2.png) [@TheRealNeil](https://api-discuss.freeagent.com/u/TheRealNeil)\
**Post date:** [13 January 2023 17:47 UTC](https://api-discuss.freeagent.com/t/oauth2-error-http-basic-access-denied/3139/1 "2023-01-13T17:47:01Z")

</div>

Hi,

I am having issues authenticating my app using Oauth2. I am able to login to the sandbox and authorise the app but then I get the error below.

 ![image](https://freeagent-api-discuss-assets-production.s3.dualstack.eu-west-1.amazonaws.com/original/1X/75ecfe983a93f961e76d79d51a5e332dbdb756c3.png)

**Gemfile**

```auto
gem "omniauth-oauth2", "~> 1.8"
gem "omniauth-rails_csrf_protection", "~> 1.0"

```

I can share more details about the strategies I created if required.  
Thanks,  
Neil

---

<div class="post-metadata">

**Author:** ![maaarghk](https://api-discuss.freeagent.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@maaarghk](https://api-discuss.freeagent.com/u/maaarghk)\
**Post date:** [15 January 2023 17:46 UTC](https://api-discuss.freeagent.com/t/oauth2-error-http-basic-access-denied/3139/2 "2023-01-15T17:46:02Z")

</div>

Looks related - [Temporary 403 responses despite required permissions](https://api-discuss.freeagent.com/t/temporary-403-responses-despite-required-permissions/3095)

POST [https://api.freeagent.com/v2/token\_endpoint](https://api.freeagent.com/v2/token_endpoint) and also POST [FreeAgent : Login](https://login.freeagent.com/sessions) are both returning awsalb-generated 403 forbidden responses. No further info is returned to debug the reason for the error, so I suspect freeagent’s AWS WAF is being a bit overzealous here blocking traffic.

---

<div class="post-metadata">

**Author:** ![James\_Green](https://api-discuss.freeagent.com/user_avatar/api-discuss.freeagent.com/james_green/32/554_2.png) [@James\_Green](https://api-discuss.freeagent.com/u/James_Green)\
**Post date:** [16 January 2023 12:41 UTC](https://api-discuss.freeagent.com/t/oauth2-error-http-basic-access-denied/3139/3 "2023-01-16T12:41:02Z")

</div>

Hello Neil,

Thank you for reaching out with your query and also thank you to Mark for your response.

After speaking to our security team they have confirmed that our web application firewall was identifying these requests as malicious and blocking them. They have since confirmed that they have run a fix for this that should resolve your issues.

Would you be able to try authenticating your app again and let me know if this is working again for you?

Many thanks,  
James

---

<div class="post-metadata">

**Author:** ![TheRealNeil](https://api-discuss.freeagent.com/user_avatar/api-discuss.freeagent.com/therealneil/32/575_2.png) [@TheRealNeil](https://api-discuss.freeagent.com/u/TheRealNeil)\
**Post date:** [16 January 2023 15:47 UTC](https://api-discuss.freeagent.com/t/oauth2-error-http-basic-access-denied/3139/4 "2023-01-16T15:47:49Z")

</div>

Thanks to both Mark and James for the responses. I spent a few more hours digging into this and discovered it was actually an error in the response processing of my app.

It appears that the omniauth-freeagent-oauth2 gem ([omniauth-freeagent-oauth2 | RubyGems.org | your community gem host](https://rubygems.org/gems/omniauth-freeagent-oauth2)) has been removed from github but I was able to download it and inspect it locally. I used this as a basis for updating my own strategy.

Here it is incase anyone else needs it.

```auto
# initializers/oauth2/freeagent.rb
# frozen_string_literal: true

require 'omniauth-oauth2'

module OmniAuth
  module Strategies
    class FreeAgent < OmniAuth::Strategies::OAuth2
      # Give your strategy a name.
      option :name, "freeagent"

      # This is where you pass the options you would pass when
      # initializing your consumer from the OAuth gem.
      option :client_options, {
        site: ENV['RAILS_ENV'] == 'production' ? "https://api.freeagent.com" : "https://api.sandbox.freeagent.com",
        authorize_url: "/v2/approve_app",
        token_url: "/v2/token_endpoint"
      }

      # You may specify that your strategy should use PKCE by setting
      # the pkce option to true: https://tools.ietf.org/html/rfc7636
      # option :pkce, true

      # These are called after authentication has succeeded. If
      # possible, you should try to set the UID without making
      # additional calls (if the user id is returned with the token
      # or as a URI parameter). This may not be possible with all
      # providers.
      uid do
        raw_info["user"]["url"]
      end

      info do
        {
          name: raw_info["user"]["name"] || "#{raw_info['user']['first_name']} #{raw_info['user']['last_name']}",
          email: raw_info["user"]["email"],
          first_name: raw_info["user"]["first_name"],
          last_name: raw_info["user"]["last_name"],
        }
      end

      extra do
        {
          raw_info: raw_info,
        }
      end

      def raw_info
        @raw_info ||= access_token.get("/v2/users/me").parsed
      end

      def callback_url
        options[:redirect_uri] || full_host + script_name + callback_path
      end
    end
  end
end

OmniAuth.config.add_camelization "freeagent", "FreeAgent"

```

and the omniauth initializer

```auto
# initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :freeagent,
           Rails.application.credentials.dig(:freeagent, :api_key),
           Rails.application.credentials.dig(:freeagent, :api_secret)
end

OmniAuth.config.logger = Rails.logger if Rails.env.development?

```

---

<div class="post-metadata">

**Author:** ![Jay\_Gregory](https://api-discuss.freeagent.com/user_avatar/api-discuss.freeagent.com/jay_gregory/32/1006_2.png) [@Jay\_Gregory](https://api-discuss.freeagent.com/u/Jay_Gregory)\
**Post date:** [21 January 2025 11:45 UTC](https://api-discuss.freeagent.com/t/oauth2-error-http-basic-access-denied/3139/5 "2025-01-21T11:45:58Z")

</div>

Hey James, this is also happening on our live application.

> [@Temporary 403 responses despite required permissions](https://api-discuss.freeagent.com/t/temporary-403-responses-despite-required-permissions/3095):
>
> Hi, Yesterday (around 11:45am UTC) the users of our application encountered an issue where their requests to the FreeAgent API were returning a 403 Forbidden response. This was despite our code or their FreeAgent account permissions having not changed at all, and they had the required permissions to access the data they were requesting. The issue seemed to rectify itself after around ~30 mins when these same requests starting returning data again, but as there was nothing posted on the status…

Despite having the correct permissions these requests are coming back with 403:  
`https://api.freeagent.com/v2/bank_accounts`  
`https://api.freeagent.com/v2/invoices?`
