user16746875
user16746875

Reputation:

Invalid API Key in OpenWeatherMap (Error 401)

I'm trying to make an API call to OpenWeatherMap. When I execute flutter run in the terminal, the response.statusCode prints 401 which is code for Invalid API Key, although I have generated the API Key in an appropriate way and it is active and copied correctly.

const APIKey = '69f9afe03f52ef5c83887fc86dd79d99';

  void getData() async {
    http.Response response = await http.get(Uri.parse('https://api.openweathermap.org/data/3.0/onecall?lat=$latitude&lon=$longitude&appid=$APIKey'));
    if (response.statusCode == 200) {
      String data = response.body;
      var decodedData = jsonDecode(data);
      print(decodedData);
    } else {
      print(
        response.statusCode,
      );
    }
  }

Geolocator package for Flutter was used to assign the Latitude and Longitude. How can the 401 Error be rectified?

Upvotes: 8

Views: 44011

Answers (4)

Ashish singh
Ashish singh

Reputation: 49

for openweather map if you are a new user and just generated your api key then its take 45 minutes to activate your key

Upvotes: 3

Shaswat_Shukla
Shaswat_Shukla

Reputation: 126

Might be a bit late to reply, but the URL you are using is no longer part of the free plan and that's why you get that error. Try using the following URL, it requires the user to input a city name and there is no need of using latitude and longitude.

https://api.openweathermap.org/data/2.5/weather?q=${*cityName*}&appid=${*API_key*}&units=metric

So, if you have a text field that takes the input for a city name, then the above fetch call would work.

Here cityName is the location entered by the user and API_key is the API key you are assigned. By having &units=metric, you will get the temperature in degree Celcius. If you want the temperature in Farenheit, use &units=imperial or if you want the temperature in Kelvin, just get rid of the &units=metric part.

So for example if you paste the following in your browser, you will get the weather in London.

api.openweathermap.org/data/2.5/weather?q=London&APPID=YOUR_API_KEY

Hope my first answer on this platform helps somebody.

Upvotes: 10

Zain Basharat Ali
Zain Basharat Ali

Reputation: 167

Go to the website and generate new api key by deleting the previous one.

You can generate as many api keys as required.

Upvotes: -1

Ninad7N
Ninad7N

Reputation: 726

checkout Using OpenWeatherMap API gives 401 error here you will find some reasons why your apikey is not working. also, it takes 2 hours for key activation.

Upvotes: 14

Related Questions