1

我目前正在开发一个使用 PayPal 处理信用卡交易的 ASP.Net MVC 4 应用程序。我已经使用 PayPal RESTFul API 完成了大部分设置,出于某种原因,PayPal 的最新版本的 SDK 只是不想工作(通过 NUGET),所以我决定使用 ASP.Net 的最新和最强大的 API 自己调用HttpClient()(反正喜欢这样更好)

我需要做的是以下(从PayPal的文档中复制)

 curl -v https://api.sandbox.paypal.com/v1/payments/payment \
 -H "Content-Type:application/json" \
 -H "Authorization:Bearer EEwJ6tF9x5WCIZDYzyZGaz6Khbw7raYRIBV_WxVvgmsG" \
 -d   '{
        "intent": "sale",
        "rest_of_the_data": "I didn't include it here"
      }'

我现在坚持的是-H "Authorization:Bearer EEwJ6tF9x5WCIZDYzyZGaz6Khbw7raYRIBV_WxVvgmsG" \这里的第三个参数是我所拥有的不起作用:

using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(                                    System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1} {2}", "Authorization", accessToken.token_type, accessToken.access_token))));
    var response = client.PostAsync<Payment>(endPoint + "/v1/payments/payment", payment, new JsonMediaTypeFormatter());
    if (response.Result.IsSuccessStatusCode)
       {
          var responseContent = response.Result.Content;
          var responseString = responseContent.ReadAsAsync<Payment>().Result;
       }
}

使用上面的代码,我得到以下响应:

Result = {StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Date: Thu, 13 Jun 2013 07:06:50 GMT
  Server: Apache-Coyote/1.1
  Content-Length: 0
  Content-Type: application/json
}}

现在我确定我传递了正确的凭据,但我相信它的格式不正确,-H "Authorization:Bearer...我认为我不应该使用client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(...,但我似乎找不到替代品来完成这项工作。我应该使用什么?

在这方面的任何帮助将不胜感激。

4

1 回答 1

0

您似乎错误地设置了标题值。尝试以下操作,看看它是否有效:

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(accessToken.access_token));
于 2013-06-13T08:30:48.640 回答