我开始颤振移动开发和有问题的http包http调用。我已经用.NET编写了包含Micorosft标识的工作API。我的目标是创建一个颤振移动应用程序,通过授权从API中获取数据。我在实现用户名-密码授权以从API获得令牌时遇到了问题。同样的API调用也适用于邮递员和我现有的Xamarin Forms应用程序。http调用的相同问题,在它的标题中,我使用从邮递员那里得到的令牌。使用VS代码,所有安装的软件包都可以在相同的颤振应用程序中从openweathermap.org中检索样本数据。我的代码是:
发布于 2022-05-22 16:55:06
Microsoft 授权请求期望application/x-www-form-urlencoded
内容。
Future<String> authorization(String username, String password) async {
final response = await http.post(
Uri.parse('https:/host/path/token'),
headers: <String, String>{
'Content-Type': 'application/x-www-form-urlencoded',
},
body: <String, String>{
'grant_type': 'password',
'username': username,
'password': password,
},
encoding: Encoding.getByName('utf-8')
);
if (response.statusCode == 200) {
return jsonDecode(response.body)['access_token'];
} else {
throw Exception(response.reasonPhrase);
}
}
发布于 2022-05-21 23:33:11
你的问题很笼统,你的问题取决于你的反应数据。当然,我没有意识到您在接收令牌或将令牌用作另一个api的头时遇到了问题!但是,由于您刚刚开始使用Flutter,我建议您使用斩波器图书馆来处理HTTP和api。很简单也很有趣。
https://stackoverflow.com/questions/72332652
复制相似问题