【问题标题】:System.TypeLoadException when submitting HTTP request through Httpclient通过 Httpclient 提交 HTTP 请求时出现 System.TypeLoadException
【发布时间】:2016-10-05 02:33:01
【问题描述】:

我正在尝试通过 Xamarin Forms 使用 PayPal RESTful API,但在尝试获取 OAuth 令牌时遇到了麻烦。这是我正在运行的代码,在 Android 上发生错误。我在此处更改了身份验证标头,因此我的客户端 ID 和密码不公开。这是我基于此 HTTP 请求的 curl 命令: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>();

【问题讨论】:

    标签: c# paypal xamarin.forms httpclient


    【解决方案1】:

    使用 application/x-www-form-urlencoded 发送 post 请求的方法是这样的:您需要这样做,而不是 TryAddWithoutValidation,然后将其作为内容包含在请求中,而不是像我之前那样包含客户端凭据.

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-04
      相关资源
      最近更新 更多