0

I have generated a request in my python code to an API however it returns a 401, not authorized in the generated code and on Insomnia however it works successfully and returns a 200 when I try accessing the endpoint on my browser.

I pass the following headers

{'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Safari/605.1.15', 'Accept-Encoding': 'gzip, deflate, br', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Connection': 'keep-alive', 'Accept-Language': 'en-CA,en-US;q=0.9,en;q=0.8'}

My code is as follows:

 headers = {
            'User-Agent': f"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Safari/605.1.15",
            'Accept-Encoding': f"gzip, deflate, br",
            'Accept-Language': f"en-CA,en-US;q=0.9,en;q=0.8",
            'Accept': f"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            'Connection': f"keep-alive"}
        self.connection.headers.update(headers)
        response = self.connection.get(uri, params=payload)
        return response

where self.connection is an OAuth1Session client from the requests-oauthlib library and params is an empty JSON

Any help would be appreciated!

  • Your browser may be caching some authentication cookie, does the same API call work in a new private browser window or if you clear cookies and site data. How do you expect to authenticate to the server with your code? – import random May 02 '22 at 05:16
  • Could you provide more details on what API you're trying to connect to? – BrokenBenchmark May 04 '22 at 04:07
  • @importrandom so the endpoint uses a different JSESSION ID it seems. When I copy the JSESSION from the browser it seems to work but not the one in the code – Shreyansh Anand May 05 '22 at 15:23
  • @BrokenBenchmark its an Electronic Medical Records API called OSCAR-JUNO, they use a standard OAUTH1a workflow and have two different set of endpoints. One set works perfectly, the second set which has content we need has the issue I describe above – Shreyansh Anand May 05 '22 at 15:24
  • The browser probably works because you've authenticated properly in the browser. You should edit you question to include a [mcve], it's difficult to help you further without more information. – import random May 06 '22 at 03:34

0 Answers0