我正在尝试进行身份验证调用,无论我尝试什么,我都会收到 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 ...