我尝试使用Apache的 HttpClient 发送以下内容(ebay API应用令牌请求) POST ,然后我继续获取 400:错误请求错误,即使我的代码看起来很好。
以下是代码:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import sun.misc.BASE64Encoder;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;
public class OrderFileDownload {
public static void main(String[] args) throws Exception {
OrderFileDownload od = new OrderFileDownload();
System.out.println(od.submitTokenRequest());
}
public String submitTokenRequest() throws Exception {
String url = "https://api.sandbox.ebay.com/identity/v1/oauth2/token";
CloseableHttpClient client = HttpClients.createDefault();
List<BasicNameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("grant_type","client_credentials"));
params.add(new BasicNameValuePair("redirect_uri","<RuRedirect-value>"));
params.add(new BasicNameValuePair("scope","https://api.ebay.com/oauth/api_scope"));
HttpPost request = new HttpPost(url);
request.addHeader("Content-Type", "application/x-www-form-urlencoded");
request.addHeader("Authorization","Basic " + encrypt64("<Client-Id>:<ClientSecret>"));
HttpEntity entity = new UrlEncodedFormEntity(params);
request.setEntity(entity);
HttpResponse response = client.execute(request);
client.close();
return response.toString();
}
private String encrypt64(String text) {
BASE64Encoder enc = new BASE64Encoder();
return enc.encode(text.getBytes());
}
}
这是方法返回的响应:
HttpResponseProxy{HTTP/1.1 400 Bad Request [Content-Type: text/plain, Content-Length: 0] [Content-Type: text/plain,Content-Length: 0,Chunked: false]}
任何人都可以帮我解决这个问题吗?
谢谢!
答案 0 :(得分:0)
我在使用ebay API时遇到了同样的问题,但是如果我在我的代码中添加重试,尝试在一分钟之后尝试,那么它对我有效。
你也可以尝试这个,但我还没有找到它的根本原因。
谢谢,
Parminder Singh