Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios {quot;cod:401,";message";:";使用Moya时开放天气API错误上的API键无效_Ios_Swift_Api_Moya - Fatal编程技术网

Ios {quot;cod:401,";message";:";使用Moya时开放天气API错误上的API键无效

Ios {quot;cod:401,";message";:";使用Moya时开放天气API错误上的API键无效,ios,swift,api,moya,Ios,Swift,Api,Moya,所以我用moya创建了一个对openweatherAPI的API请求。现在邮递员的返回似乎还可以,但是X代码上的API调用返回401:无效的API密钥 我已经尝试了很多方法来看看到底哪里出了问题,但我似乎没有找到答案 导入基础 进口莫亚 导入UIKit enum WeatherAPI{ 案例showCurrentWeather(城市名称:字符串) casedweather的案例展示(城市名称:String) } 扩展WeatherAPI:TargetType{ var任务:任务{ 切换自身{ c

所以我用moya创建了一个对openweatherAPI的API请求。现在邮递员的返回似乎还可以,但是X代码上的API调用返回401:无效的API密钥

我已经尝试了很多方法来看看到底哪里出了问题,但我似乎没有找到答案

<代码>导入基础 进口莫亚 导入UIKit enum WeatherAPI{ 案例showCurrentWeather(城市名称:字符串) casedweather的案例展示(城市名称:String) } 扩展WeatherAPI:TargetType{ var任务:任务{ 切换自身{ case.showCurrentWeather(cityName:let cityName): 让params=[“q”:cityName,“APPID”:Constants.WEATHER\u API\u KEY] requestParameters(参数:params,编码:JSONEncoding.default) case.showForcasedWeather(cityName:let cityName): 让params=[“q”:cityName,“APPID”:Constants.WEATHER\u API\u KEY] requestParameters(参数:params,编码:JSONEncoding.default) } } var baseURL:URL{ 返回URL(字符串):https://api.openweathermap.org/data/2.5")! } 变量路径:字符串{ 切换自身{ 案例.showCurrentWeather: 返回“/天气” 案例。展示案例天气: 返回“/预测” } } var方法:Moya.方法{ 切换自身{ 案例.showCurrentWeather: 回来,快 案例。展示案例天气: 回来,快 }} var sampleData:数据{ 返回数据() } 变量标题:[字符串:字符串]{ 切换自身{ 案例.showCurrentWeather: 返回[“内容类型”:“应用程序/json”] 案例。展示案例天气: 返回[“内容类型”:“应用程序/json”] } } }

适用于邮递员和浏览器

X代码返回

“cod”:401,“message”:“无效的API密钥。有关详细信息,请参阅。”}

您应该在任务变量中将此JSONEncoding.default更改为URLEncoding.default