问题描述
我正在尝试进行身份验证调用,无论我尝试什么,我都会收到 400,错误请求。这是我的代码:
try
{
HttpClient myHttpClient = new HttpClient();
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
//client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type","application/x-www-form-urlencoded");
LoginApiRequestModel loginRequestModel = new LoginApiRequestModel()
{
Email = email,Password = password,UserScope = "restapi",ClientSecret = Constants.ClientSecret,GrantType = Constants.GrantType,ClientId = "5a8f011c-xxxx-xxxx"
};
var serializedLogin = JsonConvert.SerializeObject(loginRequestModel);
var content = new StringContent(serializedLogin,Encoding.UTF8,"application/json");
var response = await myHttpClient.PostAsync("https://xxxxxx/oauth/token",content);
if (response.IsSuccessstatusCode)
{
using (var stream = await response.Content.ReadAsstreamAsync())
using (var reader = new StreamReader(stream))
using (var json = new JsonTextReader(reader))
{
var jsoncontent = _serializer.Deserialize<LoginApiResponseModel>(json);
await SecureStorage.SetAsync("accesstoken",jsoncontent.Accesstoken);
await SecureStorage.SetAsync("refreshToken",jsoncontent.RefreshToken);
await SecureStorage.SetAsync("email",email);
return jsoncontent;
}
}
else
{
var contents = response.Content.ReadAsstringAsync().Result;
Debug.WriteLine(contents);
return null;
}
}
catch (Exception ex)
{
Debug.Write(ex);
return null;
}
我尝试过使用 SendAsync、所有可能的标头和编码类型,但没有任何效果。
我什至尝试过使用 Xcode、本机 iOS 进行调用,并且可以正常工作。这是我在 iOS 中的代码:
var headers = NetworkClient.defaultHeaders()
headers.add(name: "Content-Type",value: "application/x-www-form-urlencoded")
apiRequest(.authentication,parameters: params,encoding: URLEncoding.httpBody,headers: headers) { (result) in ...
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)