Paypal .NET SDK订单授权总是404

问题描述 投票:0回答:1

当我想要获取订单或尝试授权订单时,我总是收到 404

Bad Request

我尝试了REST API方式:

var config = new Dictionary<string, string>
{{ "mode", "sandbox" }, { "clientId", "123" },{ "clientSecret", "ABC" }};

var token = new OAuthTokenCredential(config).GetAccessToken();

using var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", $"{token}");
var content = new StringContent(JsonSerializer.Serialize(string.Empty), Encoding.UTF8, "application/json");
var response = await client.PostAsync("https://api.sandbox.paypal.com/v2/checkout/orders/1234567/authorize", content, CancellationToken.None);

响应404..


我尝试了使用官方paypal lts sdk的SDK方式。在这里检查一下:

var config = new Dictionary<string, string>
{{ "mode", "sandbox" }, { "clientId", "123" },{ "clientSecret", "ABC" }};

var token =  new OAuthTokenCredential(config).GetAccessToken();
        
var orderId = "1234566";
var context = new APIContext(token);
var order = Order.Get(context, orderId);

响应404...

您可以在这里找到授权功能的官方测试:https://github.com/paypal/PayPal-NET-SDK/blob/releasinator/Source/Tests/OrderTest.cs


但是...如果我使用 cURL - 一切都很顺利。

curl -v -X POST https: //api-m.sandbox.paypal.com/v2/checkout/orders/1234566/authorize \
-H "Content-Type: application/json" \
-H "Authorization: Bearer asdasd"

通过http-get方法获取订单信息也没有问题

var response = await client.GetAsync("https://api.sandbox.paypal.com/v2/checkout/orders/2134566", CancellationToken.None);

也许有人知道一个技巧或窍门或看到我的错误。 非常感谢。

c# asp.net-core paypal paypal-sandbox paypal-rest-sdk
1个回答
0
投票

授权标头缺少前面的“bearer”。

client.DefaultRequestHeaders.Add("Authorization", $"bearer {token}");

或者你可以像下面这样设置不记名令牌

client.SetBearerToken(token);
© www.soinside.com 2019 - 2024. All rights reserved.