好的,所以我在将这些JSON请求发送到Ebay API时遇到了一些麻烦。
这是json请求:
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请求的Debug.WriteLine()
输出为:
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}]}
以下是C#中发送请求的代码:
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);
我Debug.WriteLine("response.Content = " + response.Result);
时得到的输出是:
response.Content = StatusCode:401,ReasonPhrase:'Unauthorized', 版本:1.1,内容:System.Net.Http.StreamContent,Headers:{
RlogId: t6ldssk%28ciudbq%60anng%7Fu2h%3F%3Cwk%7Difvqn * 14%3F0513%29pqtfwpu%29pdhcaj%7E%29fgg%7E%606%28dlh-1613f3af633-0xbd X-EBAY-C-REQUEST-ID:ri = HNOZE3cmCr94,rci = 6kMHBw5dW0vMDp8A
X-EBAY-C-VERSION:1.0.0 X-EBAY-REQUEST-ID: 1613f3af62e.a096c6b.25e7e.ffa2b377!/身份证/ 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:基本内容长度:77内容类型: application / json}
任何人都可以看到我出错的地方。干杯
答案 0 :(得分:0)
好的,因此身份验证失败了,因为我发送了错误的身份验证令牌。需要从应用程序令牌中获取oauth令牌。
所以不是像这样的base64编码令牌:
__set__
我需要做以下事情:
var base64string = "Basic " + System.Convert.ToBase64String(bytes);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", base64string);