简体   繁体   English

如何使用 java 中的 Jetty 请求复制此 curl 命令?

[英]How can I replicate this curl command using a Jetty request in java?

I have this curl command我有这个 curl 命令

curl -F 'client_id={client id}' \
-F 'client_secret={client secret}' \
-F 'code={temporary token}' \
-F 'grant_type=authorization_code' \
https://cloud.lightspeedapp.com/oauth/access_token.php

taken from https://developers.lightspeedhq.com/retail/authentication/access-token/取自https://developers.lightspeedhq.com/retail/authentication/access-token/

I'm trying to call this api using a jetty request in java. Here's what I've got so far.我正在尝试使用 java 中的码头请求调用此 api。这是我到目前为止所获得的。

URIBuilder uriBuilder = new URIBuilder("https://cloud.lightspeedapp.com")
  .setPath("/oauth/access_token.php");

MultiPartContentProvider contentProvider = new MultiPartContentProvider();
contentProvider.addFieldPart("grant_type", new StringContentProvider("authorization_code"), null);
contentProvider.addFieldPart("client_id", new StringContentProvider(lightspeedRId), null);
contentProvider.addFieldPart("client_secret", new StringContentProvider(lightspeedRSecret), null);
contentProvider.addFieldPart("code", new StringContentProvider(temporaryToken), null);
contentProvider.close();

Request request = httpClient.POST(uriBuilder.build())
  .content(contentProvider)
  .header("Content-Type", "multipart/form-data");

However, it's not working.但是,它不起作用。 I'm at a complete loss and not sure what I need to change here.我完全不知所措,不确定我需要在这里改变什么。

EDIT: I tried a FormContentProvider as well and got the same results.编辑:我也尝试了FormContentProvider并得到了相同的结果。

Fields fields = new Fields();
fields.put("grant_type", "authorization_code");
fields.put("client_id", lightspeedRId);
fields.put("client_secret", lightspeedRSecret);
fields.put("code",temporaryToken);
FormContentProvider contentProvider = new FormContentProvider(fields);

Okay, turns out, the problem was the setting of the "Content-Type" header on the request at the end.好的,事实证明,问题出在最后请求中“Content-Type”header 的设置。

Request request = httpClient.POST(uriBuilder.build())
  .content(contentProvider)
  .header("Content-Type", "multipart/form-data");

Without that, it worked perfectly like this.没有它,它就像这样完美地工作。

URIBuilder uriBuilder = new URIBuilder("https://cloud.lightspeedapp.com")
  .setPath("/oauth/access_token.php");

Fields fields = new Fields();
fields.put("grant_type", "authorization_code");
fields.put("client_id", lightspeedRId);
fields.put("client_secret", lightspeedRSecret);
fields.put("code",temporaryToken);
FormContentProvider contentProvider = new FormContentProvider(fields);

Request request = httpClient.POST(uriBuilder.build())
  .content(contentProvider);

The reason I was setting that header in the first place was because of this part of the documentation .我首先设置 header 的原因是因为文档的这一部分。

文档中的内容类型错误

So yeah, turns out Lightspeed just has terrible documentation.所以是的,事实证明 Lightspeed 的文档很糟糕。 Let this be a cautionary tale.让这成为一个警示故事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM