python icon indicating copy to clipboard operation
python copied to clipboard

Unexpected 403 response

Open nick-lironi-irvine opened this issue 2 years ago • 4 comments

I have integrated this library into a Python 3.6 application which runs in ECS on AWS. The library works as expected when running the application locally, and fails with a 403 response to the request when deployed into the AWS environment. I have done some debugging and this appears to not be a network issue; I can write equivalent code that calls the API using the requests library directly which returns the correct results, while making 'the same' request internally to the library fails with a 403 response.

Sample code to demonstrate this; it is a simplified version of the code running in my application

import logging
import sys
import ipinfo
import requests

ip = "<some IP address>"
try:
   # headers copied from library internals
    headers = {
        "user-agent": "IPinfoClient/Python{version}/{sdk_version}".format(
            version=sys.version_info[0], sdk_version="4.2.1"
        ),
        "accept": "application/json",
        "authorization": "Bearer <my token>",
    }

    response = requests.get(url="https://ipinfo.io/" + ip, headers=headers)
    logging.warning("Headers: %s", response.headers)
    logging.warning("Text: %s", response.text)
    response.raise_for_status() # this does not fail, the expected values are logged above
except Exception as e:
    logging.warning("manual call failed", exc_info=e)

try:
    return ipinfo.getHandler(<my token>).getDetails(ip_address=ip) # this fails
except Exception as e:
    logging.warning(
        "Failed to get IP Location Details", exc_info=e,
    )
    return None

The output of the call to the library is

File "/home/stellar/virtualenv/lib/python3.6/site-packages/ipinfo/handler.py", line 98, in getDetails
    response.raise_for_status()
File "/home/stellar/virtualenv/lib/python3.6/site-packages/requests/models.py", line 943, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://ipinfo.io/<IP address>

I would appreciate any assistance in diagnosing or fixing this issue

nick-lironi-irvine avatar Oct 07 '21 23:10 nick-lironi-irvine

@nick-lironi-irvine You could only get a 403 if your token was invalid. Can you confirm it isn't gibberish before calling handler.getDetails()? (You can check handler.access_token). Also can you confirm that the ip you're sending into the getDetails func is a string or atleast some object that when concatenated with a string gives a proper IP string?

UmanShahzad avatar Oct 09 '21 10:10 UmanShahzad

Thanks @UmanShahzad, that helped me see that it was a problem with the way the API key was being loaded and passed to the library. Can you turn this into a feature request instead? I can see now that the body of the 403 response contains a nice message like

{
 "title": "Unknown token",
 "message": "Please ensure you've entered your token correctly. Refer to https://ipinfo.io/developers for details, or contact us at [email protected] for help"
}

but I don't think this is exposed by the library; having this information in the exception that was raised would have made this much easier to debug

nick-lironi-irvine avatar Oct 10 '21 22:10 nick-lironi-irvine

We could throw a special error type APIError() which takes the JSON body as a parameter. Will add this at some point, sure!

UmanShahzad avatar Oct 11 '21 04:10 UmanShahzad

@SobanMahmood can you check this one out when free.

UmanShahzad avatar Aug 29 '22 05:08 UmanShahzad