我是新來的java,需要這個項目。我必須結合使用Apache HttpClient和FastBill Api。Apache httpclient whith fastbill.api in java
爲FastBill阿比curl命令是
curl -v -X POST \
–u {E-Mail-Adresse}:{API-Key} \
-H 'Content-Type: application/json' \
-d '{json body}' \
https://my.fastbill.com/api/1.0/api.php
我使用curl命令成功與此JSON文件
{
"SERVICE":"customer.create",
"DATA":
{
"CUSTOMER_TYPE":"business",
"ORGANIZATION":"Musterfirma",
"LAST_NAME":"Mmann"
}
}
所以,我敢肯定,我的用戶名,密碼和JSON文件加工。 FastbillApi使用http基本身份驗證。我在Java
public class Fastbill implements FastbillInterface {
private static final String URL_SECURED_BY_BASIC_AUTHENTICATION = "https://my.fastbill.com/api/1.0/api.php";
public Customer createCustomer(String firstname, String lastname, CustomerType customertype, String organisation) {
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials
= new UsernamePasswordCredentials("*****@****", "************"); //Api Username and API-Key
HttpClient client = HttpClientBuilder.create()
.setDefaultCredentialsProvider(provider)
.build();
HttpPost httpPost = new HttpPost(URL_SECURED_BY_BASIC_AUTHENTICATION);
httpPost.setHeader("Content-Type", "application/json");
String json = "{\"SERVICE\":\"customer.create\",\"DATA\":{\"CUSTOMER_TYPE\":\"business\",\"ORGANIZATION\":\"Musterfirma\",\"LAST_NAME\":\"Newmann\"}}";
try {
HttpEntity entity = new ByteArrayEntity(json.getBytes("UTF-8"));
httpPost.setEntity(entity);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpResponse response;
try {
response = client.execute(httpPost);
int statusCode = response.getStatusLine()
.getStatusCode();
System.out.println(statusCode);
String responseString = new BasicResponseHandler().handleResponse(response);
System.out.println(responseString);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
試圖以此作爲迴應,我得到
org.apache.http.client.HttpResponseException: Unauthorized
at org.apache.http.impl.client.AbstractResponseHandler.handleResponse(AbstractResponseHandler.java:70)
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:66)
at fastbillAPI.Fastbill.createCustomer(Fastbill.java:93)
at main.Mar.main(Mar.java:38)
現在,我不知道我做錯了什麼。