Je suis nouveau sur java et j'en ai besoin pour un projet. Je dois utiliser Apache HttpClient en combinaison avec FastBill Api.Apache httpclient avec fastbill.api en java
La commande Curl pour FastBill Api est
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
J'ai utilisé la commande curl avec succès avec ce fichier JSON
{
"SERVICE":"customer.create",
"DATA":
{
"CUSTOMER_TYPE":"business",
"ORGANIZATION":"Musterfirma",
"LAST_NAME":"Mmann"
}
}
Alors, je suis sûr que mon dossier nom d'utilisateur, mot de passe et JSON est travail. FastbillApi utilise l'authentification de base http. J'ai essayé en 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();
}
Comme réponse, je reçois maintenant, je
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)
aucune idée de ce que je fais mal.