Cdiscount平臺對接采坑心得

本人從事IT行業(yè)7年,其中有三年從事跨境電商業(yè)務,期間對接了不少的跨境電商平臺,頗有心得。但最近對接Cdiscount這個平臺的時候,在坑里掙扎了許久,挫敗感油然而生。

Cdiscount是目前法國最大的電子商務平臺,從它的Marketplace API來看,API這快也算是挺成熟的。文檔挺詳細的,調(diào)用參數(shù)、示例和返回值等都很詳細。但就是這么一個API文檔詳細的平臺,也存在著一些坑,在此與大家分享。

查看原文發(fā)現(xiàn)更多干貨

image

生成token

根據(jù)以往經(jīng)驗,API對接的難點有兩個,一個在前期的授權,另一個是對接過程中字段的對照。這個平臺授權的部分,還算是挺簡單的,使用的是Basic Auth方式,只需要用戶名和密碼(不是登錄系統(tǒng)的用戶名和密碼,是申請開發(fā)者后生成的),代碼如下

private void GetToken()
{
    string url = "https://sts.cdiscount.com/users/httpIssue.svc/?realm=https://wsvc.cdiscount.com/MarketplaceAPIService.svc";

    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    request.ContentType = "application/json";
    request.Method = "GET";

    string base64Credentials = GetEncodedCredentials();
    request.Headers.Add("Authorization", "Basic " + base64Credentials);
    try
    {
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        string result = string.Empty;
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            result = reader.ReadToEnd();
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(result);
            Token = xml.InnerText;
        }
    }
    catch (Exception ex)
    {
        ;
    }
}

/// <summary>
/// base64Credentials
/// </summary>
/// <returns></returns>
private string GetEncodedCredentials()
{
    string mergedCredentials = string.Format("{0}:{1}", "holystonecd-api", "HS2019hs@");
    byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
    return Convert.ToBase64String(byteCredentials);
}

具體調(diào)用

引入wcf:https://wsvc.cdiscount.com/MarketplaceAPIService.svc,命名為CdiscoundService,代碼如下:

/// <summary>
/// 獲取訂單列表
/// </summary>
/// <param name="filter"></param>
public Order[] GetOrderList(CdiscoundService.OrderFilter filter)
{
    try
    {
        CdiscoundService.HeaderMessage headerMessage = GetHeaderMessage();
        var result = sercice.GetOrderList(headerMessage, filter);
        return result.OrderList;
    }
    catch (Exception)
    {
        return null;
    }
}

/// <summary>
/// 構(gòu)造HeaderMessage參數(shù)
/// </summary>
/// <returns></returns>
private CdiscoundService.HeaderMessage GetHeaderMessage()
{
    CdiscoundService.HeaderMessage headerMessage = new CdiscoundService.HeaderMessage();
    headerMessage.Context = new CdiscoundService.ContextMessage();
    headerMessage.Context.CatalogID = 1;
    headerMessage.Context.CustomerPoolID = 1;
    headerMessage.Context.SiteID = 100;
    headerMessage.Localization = new CdiscoundService.LocalizationMessage();
    headerMessage.Localization.Country = CdiscoundService.Country.Fr;
    headerMessage.Localization.Currency = CdiscoundService.Currency.Eur;
    headerMessage.Localization.DecimalPosition = 2;
    headerMessage.Localization.Language = CdiscoundService.Language.Fr;
    headerMessage.Security = new CdiscoundService.SecurityContext();
    headerMessage.Security.TokenId = Token;
    headerMessage.Version = "1.0";
    return headerMessage;
}

具體參數(shù)可看:https://dev.cdiscount.com/marketplace/?page_id=130

OrderFilter參數(shù)

Cdiscount的坑就在這個參數(shù),一不小心就掉坑里。這個參數(shù)可通過API文檔先熟悉,這樣對后續(xù)內(nèi)容會有更深刻的理解。

時間條件不起作用

從文檔上看,可根據(jù)創(chuàng)建時間BeginCreationDate和EndCreationDate查詢,也可根據(jù)BeginModificationDate和EndModificationDate修改時間進行查詢。然而,不管顛來倒去這兩個參數(shù)怎么搞,結(jié)果還是把全部訂單都查了出來。

網(wǎng)上對于這個平臺的資料少之又少,唯一的頭緒就是說States條件得合起來用。但奇跡并沒有出現(xiàn),訂單還是全部都查出來。到此,只能暫時放棄,因為還有另一個坑。

查詢不到訂單明細

從文檔上看,只需將FetchOrderLines這個值設置為true,即可獲取訂單明細,可實際上并非如此,這個參數(shù)怎么設都沒有效果。

遇到兩個坑,不得不求助Cdiscount客服,可經(jīng)過2天漫長的等待,客服丟了兩個PHP調(diào)用的示例,對比了一下,發(fā)現(xiàn)跟自己的傳參并無出入。沒辦法,只能把入?yún)ⅲ祷刂档热看虬?,再發(fā)給客服,希望能有進一步的回應??勺罱K等來的還是這兩個PHP調(diào)用示例。

客服是沒有指望了,只能自己繼續(xù)研究,意外的發(fā)現(xiàn)了幾個神秘參數(shù),BeginCreationDateSpecified、EndCreationDateSpecified和FetchOrderLinesSpecified,分別與BeginCreationDate、EndCreationDate和FetchOrderLines一一對應,這幾個參數(shù)是bool類型,這或許是每個參數(shù)的開關,于是都設置為true.奇跡真的發(fā)生了,時間條件生效,明細也出來了。

總結(jié)

這個接口調(diào)通后,其他接口的問題也自然都解決了??又饕褪莾蓚€地方,一是時間條件要跟狀態(tài)條件一起用,時間才能生效。二是每個參數(shù)對應的Specified字段要設置為true,對應的字段才能生效。這些在文檔都沒體現(xiàn),客服也不會跟你說,全靠自己摸索。

源代碼

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容