系统.通过Httpclient提交HTTP请求时出现typeeloadeexception



我试图通过Xamarin表单使用PayPal RESTful API,当我试图获得OAuth令牌时,我遇到了麻烦。这是我正在运行的代码,在Android上发生错误。我在这里更改了身份验证头,这样我的客户机id和secret就不能公开使用了。这是curl命令,我也基于此HTTP请求:https://developer.paypal.com/docs/integration/direct/make-your-first-call/。谢谢你。

var getToken = new HttpClient(new NativeMessageHandler());
getToken.BaseAddress = new Uri("https://api.sandbox.paypal.com/v1/oauth2/token");
getToken.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
getToken.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue("en_US"));
getToken.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("**client_id**", "**secret**");
getToken.DefaultRequestHeaders.TryAddWithoutValidation("content-type", "application/x-www-form-urlencoded");
OAuthDetails clientCredentials = new OAuthDetails("client_credentials");
HttpResponseMessage tokenResponse = await getToken.PostAsJsonAsync("/v1/oauth2/token", clientCredentials);
AccessResponse accessInfo = await tokenResponse.Content.ReadAsAsync<AccessResponse>();

用application/x-www-form-urlencoded发送post请求的方式是这样的:你需要这样做,而不是TryAddWithoutValidation,然后将其作为请求的内容,而不是像我以前那样的客户端凭据。

var tokenContent = new FormUrlEncodedContent(new[]
{
    new KeyValuePair<string, string>("grant_type", "client_credentials"
});