Fedex API 不返回运费 C#

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

我正在尝试使用以下 API 获取联邦快递的运费,并且我正在使用 C#

https://developer.fedex.com/api/en-sg/catalog/rate/v1/docs.html

但是我收到此错误,下面是我的完整代码和错误。

首先,我调用 OAuth API 并生成令牌,我成功获得了令牌,然后我调用 API 来获取费率,但仍然无法正常工作。所以对此有什么想法吗?

错误:

{StatusCode: 403, ReasonPhrase: 'Forbidden', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
  Server: Layer7-API-Gateway
  Date: Wed, 18 Jan 2023 11:09:22 GMT
  Connection: close
  Server-Timing: cdn-cache; desc=MISS
  Server-Timing: edge; dur=615
  Server-Timing: origin; dur=67
  Content-Encoding: gzip
  Content-Type: application/json;charset=UTF-8
  Content-Length: 182
}}

完整代码:

var client = new HttpClient();

string clientid = _configuration["FedEx:ClientId"];
string clientsecret = _configuration["FedEx:SecretKey"];

var url = "https://apis-sandbox.fedex.com/oauth/token";

var parametersAuth = new Dictionary<string, string>();
parametersAuth.Add("grant_type", "client_credentials");
parametersAuth.Add("client_id", clientid);
parametersAuth.Add("client_secret", clientsecret);

var reqAuth = new HttpRequestMessage(HttpMethod.Post, url) { Content = new FormUrlEncodedContent(parametersAuth) };
var resAuth = await client.SendAsync(reqAuth);
var authDetailsJson = await resAuth.Content.ReadAsStringAsync(); // here I am getting toekn successfully.

if (resAuth.StatusCode == System.Net.HttpStatusCode.OK)
{
    var authDetails = JsonConvert.DeserializeObject<dynamic>(authDetailsJson);

    JObject objRateContent = JObject.FromObject(new
    {
        AccountNumber = JObject.FromObject(new
        {
            Value = "My account number I am placing here"
        }),
        RequestedShipment = JObject.FromObject(new
        {
            Shipper = JObject.FromObject(new
            {
                Address = JObject.FromObject(new
                {
                    City = "Anchorage",
                    StateOrProvinceCode = "AK",
                    PostalCode = "99504",
                    CountryCode = "US"
                })
            }),
            Recipient = JObject.FromObject(new
            {
                Address = JObject.FromObject(new
                {
                    City = "Kenai",
                    StateOrProvinceCode = "AK",
                    PostalCode = "99611",
                    CountryCode = "US"
                })
            }),
            PickupType = "CONTACT_FEDEX_TO_SCHEDULE",
            RequestedPackageLineItem = JObject.FromObject(new
            {
                Weight = JObject.FromObject(new
                {
                    Units = "KG",
                    Value = 25
                })
            })
        })
    });

    var input = JsonConvert.SerializeObject(objRateContent);

    var urlquote = "https://apis-sandbox.fedex.com/rate/v1/rates/quotes";

    var request = new HttpRequestMessage(HttpMethod.Post, urlquote);

    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    request.Headers.Add("X-locale", "en_US");
    request.Headers.Add("Authorization", "Bearer " + authDetails.access_token);

    request.Content = new StringContent(input, Encoding.UTF8, "application/json");

    request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    var response = await client.SendAsync(request);
    var content = await response.Content.ReadAsStringAsync();
c# asp.net-core asp.net-web-api fedex
1个回答
0
投票

也许你找到了解决方案?

谢谢,

© www.soinside.com 2019 - 2024. All rights reserved.