You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Flutter调用OpenWeatherMap API返回401状态码问题求助

解决Flutter天气应用调用OpenWeatherMap API返回401状态码的问题

Hey there! Let's figure out why you're hitting that 401 error when switching to your device's current location in your Flutter weather app—this is a super common pitfall with API integrations, so let's break it down step by step.

First off, let's clarify: a 401 status code means "Unauthorized"—the API doesn't recognize your request as valid, and this has nothing to do with JSON parsing (since you said direct URL access works fine, your app's JSON handling is totally okay).

Here are the most likely reasons and fixes:

  • You're missing the API key in the request when using device location
    When you used the simulator's default location, you probably hard-coded the full URL with your appid parameter. But when switching to device location, maybe you forgot to append the API key when building the request URL with the new latitude/longitude.
    Double-check your network request code: for example, if you have a function like getWeather(double lat, double lon), make sure it's constructing the URL like this:

    final url = Uri.parse('https://api.openweathermap.org/data/2.5/weather?lat=$lat&lon=$lon&appid=YOUR_API_KEY&units=metric');
    

    Compare this to the code you used for the default location—ensure the appid parameter is present in both cases.

  • Incorrect parameter name for the API key
    OpenWeatherMap requires the API key to be passed via the appid query parameter, not api_key or any other name. It's easy to mix this up when rewriting code for device location.
    Use Flutter DevTools' Network tab (or just print the final URL to the console) to inspect the exact request your app is sending. If the appid parameter is missing or misnamed, that's the culprit.

  • API key restrictions or quota issues
    Even if direct browser access works, check your OpenWeatherMap account dashboard:

    • Is your API key enabled? Sometimes keys get disabled if you haven't used them in a while.
    • Have you hit your request quota? Free tier limits can be easy to hit during development.
    • Do you have IP restrictions set on the key? If your browser uses a different IP than your emulator/device, that could block the request.
  • URL encoding problems
    When concatenating latitude and longitude values into the URL, make sure special characters (like negative signs) are properly encoded. Use Uri.parse() instead of raw string concatenation to avoid this—Uri.parse() automatically handles encoding for you, so you don't have to worry about malformed URLs causing the API to miss your key parameter.

Remember: since direct URL access works, the issue is definitely in how your Flutter app is constructing the request when using device location—not JSON parsing. Focus on comparing the two request scenarios, and you'll find the fix quickly!

内容的提问来源于stack exchange,提问作者Harsh Sani

火山引擎 最新活动