403 error returned from python get requests, but auth works in postman?

Viewed 904

I'm trying to return a GET request from an API using HTTPBasicAuth.

I've tested the following in Postman, and received the correct response

URL:"https://someapi.data.io"
username:"username"
password:"password"

And this returns me the data I expect, and all is well.

When I've tried this in python however, I get kicked back a 403 error, alongside a

""error_type":"ACCESS DENIED","message":"Please confirm api-key, api-secret, and permission is correct."

Below is my code:

import requests
from requests.auth import HTTPBasicAuth

URL = 'https://someapi.data.io'
authBasic=HTTPBasicAuth(username='username', password='password')
r = requests.get(URL, auth = authBasic)
print(r)

I honestly can't tell why this isn't working since the same username and password passes in Postman using HTTPBasicAuth

2 Answers

You have not conveyed all the required parameters. And postman is doing this automatically for you.

To be able to use in python requests just specify all the required parameters.

headers = {
    'Host': 'sub.example.com',
    'User-Agent': 'Chrome v22.2 Linux Ubuntu',
    'Accept': '*/*',
    'Accept-Encoding': 'gzip, deflate, br',
    'Connection': 'keep-alive',
    'X-Requested-With': 'XMLHttpRequest'
}
url = 'https://sub.example.com'
response = requests.get(url, headers=headers)

It could be due to the fact that the user-agent is not defined

try the following:

import requests
from requests.auth import HTTPBasicAuth

URL = 'https://someapi.data.io'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36'}
authBasic=HTTPBasicAuth(username='username', password='password')
r = requests.get(URL, auth = authBasic, headers=headers)
print(r)
Related