2

I am using openweathermap to get the weather data and detect the user's location and display the data...When I use it in localhost, I get the correct location, but when I deploy it to heroku, it shows the location as Ashburn...How to fix this?

I am using this for getting the user's location : https://ip-geolocation.whoisxmlapi.com/api/v1?apiKey=myApiKey

url1='https://ip-geolocation.whoisxmlapi.com/api/v1?apiKey=MyApiKey'
res = requests.request('GET', url1)
Jdata = res.json()
city = Jdata['location']['city']
completed_url = "http://api.openweathermap.org/data/2.5/weather? 
appid=myapiid&q="+city

r = requests.get(completed_url)
abcd = r.json()
abcdmain = abcd['main']

https://apiappanirudh.herokuapp.com/

Visit the above link for the app..

I want the app to detect the user's location and then display that...What is the reason behind the location changing and how to fix it?

Note: I am using flask.

Thanks.

  • 1
    I think you will need get the user IP address and specify it in the API query... https://ip-geolocation.whoisxmlapi.com/api. From the tag I understand you are using flask. You can find then user’s IP like so https://stackoverflow.com/questions/3759981/get-ip-address-of-visitors-using-flask-for-python – CloudBalancing Dec 28 '20 at 18:12
  • The users IP address you can get using the SO link i provided in my previous comment. Same goes for the query link is provided. From a quick look youll need to add to the url “&ip=theUsersIP” – CloudBalancing Dec 28 '20 at 18:19
  • Hey, Unfortunately, that is not working...The ip address is giving me an empty city.. –  Dec 28 '20 at 18:51
  • What happens when you put the IP in their website directly? Do you get the same response? – CloudBalancing Dec 29 '20 at 08:58
  • When I put the ip address in the url, I get the correct one...But after deploying, it automatically changes the location to ashburn again...Deploying in heroku...Also just found that the API does not work after deploying...It works in local host...Most likely bcz of some error in the weather... –  Dec 29 '20 at 11:27

1 Answers1

0

When using an IP geolocation API, you should distinguish 2 scenarios: whether you call your geolocation API from the client-side and from the server-side.

In the former case, it means you are querying the IP geolocation API from the client browser, using JavaScript for instance. In that case, most geolocation APIs provide an origin endpoint that you can invoke without specifying any IP address. You will get information for the IP address associated with the client running the browser. Here is an example using Ipregistry:

https://api.ipregistry.co/?key=tryout&pretty=true

In the latter case, you are receiving a request to your server and you are invoking the IP geolocation API from your server. It could be using PHP, NodeJS, Java, whatever programming language behind an application server or proxy. If we draw a diagram flow, this looks as follows:

A client --> Your server --> IP geolocation API

If you invoke, an IP geolocation API without specifying more information, the best that can be done by the geolocation API is to discover what is the IP address behind your server (i.e. from where the request originates). That's the issue you described and what you experienced.

Hopefully, most IP geolocation APIs include an endpoint where you can specify what IP address to lookup data for. So when you receive a request from a client on your server, you need to extract the client IP address from your request and then use a dedicated IP geolocation API endpoint that allows passing an IP address.

How to extract the client IP address from a request you receive on your server? This is specific to your application. That's why there is no one fit all solution. Most of the time there is a request header that includes the value you are looking for. For instance, the header X-Forwarded-For is quite common but not standard. In PHP you need to use $_SERVER['REMOTE_ADDR'].

Let's say you identify your client IP address as being 200.77.121.230, then using Ipregistry, you would get location and threat data with an HTTP GET request to the next endpoint:

https://api.ipregistry.co/200.77.121.230?key=tryout&pretty=true

Laurent
  • 14,122
  • 13
  • 57
  • 89