flutterapi-keyhttp-status-code-401openweathermap

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?


Solution

  • 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.