当前位置: 首页 > 知识库问答 >
问题:

向ebay API发送JSON请求

白驰
2023-03-14
string jsonInventoryRequest = "{" +
                    "\"requests\": [";

        int commaCount = 1;
        foreach (var ep in productsToProcess)
        {
            jsonInventoryRequest += "{\"offers\": [{" +
                "\"availableQuantity\":" + ep.EbayProductStockQuantity + "," +
                "\"offerId\":\"" + ep.EbayID.ToString() + "\"," +
                "\"price\": {" +
                    "\"currency\": \"AUD\"," +
                    "\"value\":\"" + ep.EbayProductPrice.ToString() + "\"" +
                "}" +
            "}],";

            jsonInventoryRequest += "\"shipToLocationAvailability\": " + "{" +
                "\"quantity\":" + ep.EbayProductStockQuantity +
                "},";

            jsonInventoryRequest += "\"sku\": " + ep.EbayProductSKU.ToString() + "}";
            if (commaCount < productsToProcess.Count())
                jsonInventoryRequest += ",";

            commaCount++;
            sendEbayApiRequest = true;
        }

        jsonInventoryRequest += 
            "]" +
        "}";
json string = {"requests": [{"offers": [{"availableQuantity":0,"offerId":"098772298312","price": {"currency": "AUD","value":"148.39"}}],"shipToLocationAvailability": {"quantity":0},"sku": 135779},{"offers": [{"availableQuantity":1,"offerId":"044211823133","price": {"currency": "AUD","value":"148.39"}}],"shipToLocationAvailability": {"quantity":1},"sku": 133607}]}
var ebayAppIdSetting = _settingService.GetSettingByKey(
                            "ebaysetting.appid", "");

        var ebayCertIdSetting = _settingService.GetSettingByKey(
                            "ebaysetting.certid", "");

        var ebayRuNameSetting = _settingService.GetSettingByKey(
                            "ebaysetting.appid", "");

        var stringToEncode = ebayAppIdSetting + ":" + ebayCertIdSetting;

        HttpClient client = new HttpClient();
        byte[] bytes = Encoding.UTF8.GetBytes(stringToEncode);
        var base64string = "Basic " + System.Convert.ToBase64String(bytes);
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", base64string);

        var stringContent = "?grant_type=client_credentials&" + "redirect_uri=" + ebayRuNameSetting + "&scope=https://api.sandbox.ebay.com/oauth/api_scope";
        var requestBody = new StringContent(stringContent.ToString(), Encoding.UTF8, "application/json");
        requestBody.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
        var response = client.PostAsync("https://api.sandbox.ebay.com/identity/v1/oauth2/token", requestBody);

response.content=statuscode:401,reasonprace:'unauthorized',version:1.1,content:system.net.http.streamcontent,header:{
rlogid:t6ldssk%28ciudbq%60anng%7fu2h%3f%3cwk%7difvqn*14%3f0513%29pqtfwpu%29pdhcaj%7e%29fgg%7e%606%28dlh-1613f3af633-0xbd X-eBay-request-id:1613f3af62e.a096c6b.25e7e.ffa2b377!/identity/v1/oauth2/!10.9.108.107!r1esbngcos[]!token.unknown_grant!10.9.107.168!r1oauth-envadvcdhidzs5k[]连接:保持活动日期:周一,2018年1月29日00:04:44 GMT;set-cookie:eBay=%5esbf%3d%23%5e;domain=.ebay.com;path=/
www-authenticate:Basic

有人能看出我哪里出了问题吗。干杯

共有1个答案

空夕
2023-03-14

好的,所以身份验证失败是因为我发送了错误的身份验证令牌。需要从应用程序令牌中获取oauth令牌。

因此,与base64编码标记不同,如下所示:

var base64string = "Basic " + System.Convert.ToBase64String(bytes);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", base64string);

我需要做以下几点:

url = "https://api.sandbox.ebay.com/sell/inventory/v1/bulk_update_price_quantity";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        request.Method = "POST";
        //request.ContentType = "application/json; charset=utf-8";
        request.Headers.Add("Authorization", "Bearer **OAUTH TOKEN GOES HERE WITHOUT ASTERIKS**");

        // Send the request
        using (var streamWriter = new StreamWriter(request.GetRequestStream()))
        {
            streamWriter.Write(jsonInventoryRequest);
            streamWriter.Flush();
            streamWriter.Close();
        }

        // Get the response
        HttpWebResponse response = (HttpWebResponse) request.GetResponse();
        if (response != null)
        {
            using (var streamReader = new StreamReader(response.GetResponseStream()))
            {
                // Parse the JSON response
                var result = streamReader.ReadToEnd();
            }
        }
 类似资料:
  • 我在纠结HttpURLConnection和OutputStreamWriter。 代码实际上到达了服务器,因为我确实得到了有效的错误响应。发出POST请求,但服务器端未接收到数据。 任何关于正确使用这种东西的提示都是非常感谢的。 代码在异步任务中 我尝试发送的JSON: 我从服务器得到的回应是: 我应该得到的回应是: 服务器端 PHP 脚本: 以及 Android Studio 中的 logca

  • 我在烧瓶中设置了一个非常简单的帖子路线,如下所示: 这是我试图从命令行发送的curl请求: 但是,它仍然打印出“收到的数据:“无”。所以,它不识别我传递的JSON。 在这种情况下是否需要指定json格式?

  • 问题内容: 我是Web服务的新手,正在尝试使用python脚本发送以下基于JSON的请求: 如果我将以上内容粘贴到浏览器中,它将按预期工作。但是,我正在努力从Python发送请求。以下是我正在尝试的: 如何将apikey数据添加到请求中? 谢谢! 问题答案: 代替使用urllib2,您可以使用request。这个新的python lib确实写得很好,并且使用起来更加简单直观。 要发送json数据,

  • javax.net.ssl.sslhandShakeException:收到致命警报:在com.ibm.jsse2.O.A(O.java:8)在com.ibm.jsse2.sslsocketimpl.B(SSLSocketimpl.java:40)在com.ibm.jsse2.sslsocketimpl.A(SSLSocketimpl.java:554)在com.ibm.jsse2.sslsock

  • XMLHttpRequest 对象用于和服务器交换数据。 向服务器发送请求 如需将请求发送到服务器,我们使用 XMLHttpRequest 对象的 open() 和 send() 方法:xmlhttp.open("GET","ajax_info.txt",true); xmlhttp.send(); 方法 描述 open(method,url,async) 规定请求的类型、URL 以及是否异步处理

  • 问题内容: 我正在使用下面的代码发送http POST请求,该请求将对象发送到WCF服务。可以,但是如果我的WCF服务还需要其他参数怎么办?如何从Android客户端发送它们? 这是我到目前为止编写的代码: 问题答案: 使用POST发布参数: 你错过的部分在以下内容中,即如下。 其余的事情你都可以做到。