Search code examples
ruby-on-railsrubyoauth-2.0httpartybasecamp

How can I use a oauth2 token to create a connection with HTTParty


I am trying to connect to the Basecamp API using HTTParty, I am however getting an issue with authentication.

I am getting an error "HTTP Basic: Access denied", which doesn't make sense when I am trying to do OAuth.

The class looks a bit like this (The XXXX comes from other places, but is just hardcoded for the example here).

 class Basecamp
    include HTTParty

    def initialize(oauth_token)
        self.class.base_uri "https://basecamp.com/XXXXX/api/v1/"
        @options = {
            headers: {
                "Authorization" => oauth_token,
                "User-Agent" => 'XXXX (XXXXXX)'
            }
        }
    end

    def projects
        self.class.get('/projects.json', @options)
    end

    def users
        self.class.get('/people.json', @options)
    end
end

Solution

  • According to the documentation, the header should be

    Authorization: Bearer YOUR_OAUTH_TOKEN
    

    in your case, assuming oauth_token is the token, you are passing

    Authorization: YOUR_OAUTH_TOKEN
    

    Change the header to

    "Authorization" => "Bearer #{oauth_token}",