如何为eBay浏览API函数创建HTTP标头

时间:2019-05-17 11:37:41

标签: delphi indy ebay-api idhttp

我正在尝试通过eBay的Browse API获得产品评论等级。这是我为此功能编写的代码:

sHeaders := TStringList.Create;
sHeaders.Add('X-EBAY-C-ENDUSERCTX=' + '"affiliateCampaignId=' + '533xxxxxxx"');
objIdHTTP.Request.CustomHeaders.AddStdValues(sHeaders);
sResponse := objIdHTTP.Get('https://api.sandbox.ebay.com/buy/browse/v1/item/v1|123456789|0');

但是当我测试此代码时,出现了套接字错误10054。

客户标题部分可能有问题,但是我不知道如何使它起作用。

有人可以帮助我解决任何错误吗?

这里是我所指的eBay API文档的链接:

Browse API | Use request headers

更新:这是我的更新代码:

objIdHTTP.Request.CustomHeaders.AddValue('X-EBAY-C-ENDUSERCTX', 'affiliateCampaignId=' + '533xxxxxxx');
sResponse := objIdHTTP.Get('https://api.sandbox.ebay.com/buy/browse/v1/item/v1|123456789|0');

它仍然有相同的错误。

我在Postman中测试了URL链接,并收到如下错误消息:

{
  "errors": [
      {
        "errorId": 1002,
        "domain": "OAuth",
        "category": "REQUEST",
        "message": "Missing access token",
        "longMessage": "Access token is missing in the Authorization HTTP request header."
      }
  ]
}

存在许多ID,例如AppID,DevID,CerID,OAuthToken(9583个字符),商店令牌(3343个字符),EPN广告系列(已包含在此URL标头中)。

我尝试使用X-EBAY-API-IAF-TOKEN: OAuthToken,但它回复了相同的错误消息。

更新

尤其感谢Remy Lebeau在StackOverflow中提供的所有帮助。最终我得到如下工作代码:

with objHTTP.Request.CustomHeaders do begin
  Clear;
  FoldLines := False;
  Values['Content-Type'] := 'application/x-www-form-urlencoded';
  Values['Authorization'] := 'Basic ' + 'myAuthCode';
end;
xRequestBody := TStringStream.Create('{"Authorization":"Basic ' + 'myAuthCode' + '"' + ','
                                   + ' "Content-Type:' + 'application/x-www-form-urlencoded' + '"}',
                                     TEncoding.UTF8);

sResponse := objHTTP.Post(sURL, xRequestBody);

我仍然不知道为什么Indy HTTP需要两次分配ContentType和Authorization,但是这些代码现在对我来说很好。

更新:请参阅Remy Lebeau的建议,将ContentType分配给idHTTP。直接请求并重新更新代码,如下所示:

with objHTTP.Request do begin
  Clear;
  ContentType := 'application/x-www-form-urlencoded';
  CustomHeaders.FoldLines := False;
  CustomHeaders.Values['Authorization'] := 'Basic ' + myAuthCode;
end;
xRequestBody := TStringStream.Create('{"Authorization":"Basic ' + myAuthCode + '"}', TEncoding.UTF8);

sResponse := objHTTP.Post(sURL, xRequestBody);

让我们回到我的起点。现在,我开始与GET争夺授权,将代码从第一部分修改为那些部分(再次更新代码以捕获http请求):

objHTTP.Intercept := xIdLogDebug;
with objHTTP.Request.CustomHeaders do begin
  Clear;
  FoldLines := False;
  Values['X-EBAY-C-ENDUSERCTX'] := 'affiliateCampaignId=' + '533xxxxxxx';
  Values['Authorization'] := 'Bearer ' + myToken;
  Values['Postman-Token'] := '65546b71-aef8-422e-916d-93b75ddd9de2,a0687ba7-e142-4849-8288-a8f89b66f253';
  Accept := '*/*';
  UserAgent := 'PostmanRuntime/7.13.0';
  CacheControl := 'no-cache';
  AcceptEncoding := 'gzip, deflate';
  Connection := 'keep-alive';
end;

with objHTTP do begin
  AllowCookies := True;
  CookieManager  := objCookie;
end;

Result := objHTTP.Get('https://api.sandbox.ebay.com/buy/browse/v1/item/v1|123456789|0');

它仍然出现Socket Error10054。我在Postman中测试了此URL,并且工作正常。我唯一要指出的是,Postman内部有一个用于授权的TYPE选项,它被选为“ OAuth 2.0”。

那么,如何为GET分配授权给Indy HTTP?可能类似于POST中的RequestBody。

更新

这里是通过IndyHTTP的POST调用接收到的数据:

HTTP/1.1 200 OK
Content-Length: 1905
Date: Sat, 01 Jun 2019 11:09:08 GMT
RlogId: t6ldssk%28ciudbq%60anng%7Fu2h%3F%3Cwk%7Difvqn*14%3F0512%29pqtfwpu%29pdhcaj%7E%29fgg%7E%606%28dlh-16b12ba9d25-0x4fd7e
Set-Cookie: ebay=%5Esbf%3D%23%5E;Domain=.ebay.com;Path=/
Set-Cookie: dp1=bu1p/QEBfX0BAX19AQA**5ed39054^;Domain=.ebay.com;Expires=Mon, 31-May-2021 11:09:08 GMT;Path=/
X-Content-Type-Options: nosniff
X-EBAY-C-VERSION: 1.0.0
X-EBAY-REQUEST-ID: 16b12ba9d1f.a0962ac.25e7e.ff9d75b3!/identity/v1/oauth2/!10.9.98.172!esbnewesbngcos[]!token.clientcredentials!10.9.107.144!r1oauth-envadvcdhidzs5k[IdentityCDSClient[!ClientDetailResourceV1.getClientDetail!10.199.16.86!r1oauthclnt-envjiwv5gtvevlbi[]]]
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Content-Type: application/json
Connection: keep-alive

{"access_token":"v^1.1#i^............","expires_in":7200,"token_type":"Application Access Token"}

这是通过IndyHTTP的GET调用(在POST之后运行)捕获的请求(从TIdBytes转换为String):

GET /buy/browse/v1/item/v1|123456789|0 HTTP/1.1
Content-Type: application/json
X-EBAY-C-ENDUSERCTX: affiliateCampaignId=533xxxxxxx
Authorization: Bearer v^1.1#i^............
Postman-Token: 65546b71-aef8-422e-916d-93b75ddd9de2,a0687ba7-e142-4849-8288-a8f89b66f253
Host: api.sandbox.ebay.com
Accept: */*
Accept-Encoding: gzip, deflate, identity
User-Agent: PostmanRuntime/7.13.0
Cookie: dp1=bu1p/QEBfX0BAX19AQA**5ed3395b^; ebay=%5Esbf%3D%23%5E

这是从邮递员那里复制的代码:

GET /buy/browse/v1/item/v1|123456789|0 HTTP/1.1
Host: api.sandbox.ebay.com
X-EBAY-C-ENDUSERCTX: affiliateCampaignId=533xxxxxxx
Authorization: Bearer v^1.1#i^............
User-Agent: PostmanRuntime/7.13.0
Accept: */*
Cache-Control: no-cache
Postman-Token: 65546b71-aef8-422e-916d-93b75ddd9de2,a0687ba7-e142-4849-8288-a8f89b66f253
Host: api.sandbox.ebay.com
cookie: ebay=%5Esbf%3D%23%5E; dp1=bu1p/QEBfX0BAX19AQA**5ed2398a^bl/AU60b36d0a^; s=; nonsession=CgADKACBmVweKZWYwZThjOGQxNmEwYWMxZTQ2MDNlNGJhZmZkYjQzNTT+86sJ
accept-encoding: gzip, deflate
Connection: keep-alive
cache-control: no-cache

我正在努力使IndyHTTP从服务器收到#10054错误。

谢谢。

0 个答案:

没有答案