Jump to content

Problema con Web Service Prestashop y java


ennety

Recommended Posts

Buenos días, estoy intentando realizar cambios en mi base de datos prestashop (en local), mediante java y el web service de prestashop. Lo que ocurre es que todo el rato me salta un error 401 (Unauthorized). Quiero imaginar que el problema es de la conexión entre java y la base de datos, o una falta de configuración en el panel de prestashop. Os adjunto el código java muchas gracias:

 

public String insertUser() throws IOException {
try
{
URL url = new URL("http://localhost/prestashop/api/customers");
System.out.println(url);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
//autenticacion para la API de PS
String userpass = "ICYA8NMFHHPZDKDDQE13N9ZJUH8B4GFG";
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
con.setRequestProperty ("Authorization", basicAuth);
con.setDoInput(true);
con.setDoOutput(true);
con.setConnectTimeout( 20000 ); // long timeout, but not infinite
con.setReadTimeout( 20000 );
con.setUseCaches (false);
con.setDefaultUseCaches (false);
// tell the web server what we are sending
con.setRequestProperty ( "Content-Type", "text/xml" );
con.setRequestMethod("POST");

OutputStreamWriter writer = new OutputStreamWriter( con.getOutputStream() );
// Generamos el xml con la estructura para insertar un cliente en PS
writer.write( "<?xml version='1.0' encoding='UTF-8'?>"
+ "<prestashop xmlns:xlink='http://www.w3.org/1999/xlink'>"
+ "<customer>"
+ "<id>3</id>"
+ "<id_default_group>3</id_default_group>"
+ "<id_lang>1</id_lang>"
+ "<newsletter_date_add>3</newsletter_date_add>"
+ "<ip_registration_newsletter>3</ip_registration_newsletter>"
+ "<last_passwd_gen>3</last_passwd_gen>"
+ "<secure_key>3</secure_key>"
+ "<deleted>3</deleted>"
+ "<passwd>5464646</passwd>"
+ "<lastname>naudin</lastname>"
+ " <firstname>Gerardo</firstname>"
+ "<email>[email protected]</email>"
+ "<id_gender>3</id_gender>"
+ "<birthday>19[spam-filter]09-02</birthday>"
+ "<newsletter>3</newsletter>"
+ "<optin>3</optin>"
+ "<website>3</website>"
+ "<company>3</company>"
+ "<siret>3</siret>"
+ "<ape>3</ape>"
+ "<outstanding_allow_amount>3</outstanding_allow_amount>"
+ "<show_public_prices3></show_public_prices>"
+ "<id_risk>3</id_risk>"
+ "<max_payment_days>3</max_payment_days>"
+ "<active>3</active>"
+ "<note>3</note>"
+ "<id_shop>1</id_shop>"
+ "<id_shop_group>3</id_shop_group>"
+ "<date_add>2016-07-12 09:05:40</date_add>"
+ "<date_upd>2016-07-12 09:05:45</date_upd>"
+ "<associations>"
+ " <groups>"
+ "<group>"
+ "<id></id>"
+ "</group>"
+ "</groups>"
+ "</associations>"
+ "</customer>"
+ "</prestashop>" );
writer.flush();
writer.close();
//Leemos la respuesta
InputStreamReader reader = new InputStreamReader( con.getInputStream() );
StringBuilder buf = new StringBuilder();
char[] cbuf = new char[ 2048 ];
int num;
while ( -1 != (num=reader.read( cbuf )))
{
buf.append( cbuf, 0, num );
}
String result = buf.toString();
return "\nRespuesta del servidor POST:\n" + result ;
}
catch( Throwable t )
{
t.printStackTrace( System.out );
return "Fallo";
}
}

}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...