web services - 401 Error when connecting to PrestaShop webservice from android -
i trying call webservice in prestashop 401 not authorized error. though have passed username key. tried authenticator error httpretryingerror.
find below code snippet of have done.
method one:
final string username = "key_here"; final string password = "";// leave empty url urltorequest = new url(urlstr); urlconnection = (httpurlconnection) urltorequest.openconnection(); urlconnection.setdooutput(true); urlconnection.setrequestmethod("get"); urlconnection.setrequestproperty("content-type", "text/xml;charset=utf-8"); string authtobytes = username + ":" + password; //.... byte[] authbytes = org.apache.commons.codec.binary.base64.encodebase64(authtobytes.getbytes()); string authbytesstring = new string(authbytes); //then code urlconnection.setrequestproperty("authorization","basic " + authbytesstring); int statuscode = urlconnection.getresponsecode(); ---> 401 if (statuscode != httpurlconnection.http_ok) { // throw exception inputstream in = new bufferedinputstream(urlconnection.getinputstream()); log.e("tag", getresponsetext(in)); }
method two
final string username = "keyvaluehere"; final string password = "";// leave empty string authtobytes = username + ":" + password; byte[] authbytes = org.apache.commons.codec.binary.base64.encodebase64(authtobytes.getbytes()); string authbytesstring = new string(authbytes); httpparams httpparameters = new basichttpparams(); // set timeout in milliseconds until connection established. // default value zero, means timeout not used. int timeoutconnection = 30000; httpconnectionparams.setconnectiontimeout(httpparameters, timeoutconnection); // set default socket timeout (so_timeout) // in milliseconds timeout waiting data. int timeoutsocket = 30000; httpconnectionparams.setsotimeout(httpparameters, timeoutsocket); httpclient httpclient = new defaulthttpclient(httpparameters); httpget httpget = new httpget(url); httpget.setheader("authorization","basic " +authbytesstring); try { httpresponse response = httpclient.execute(httpget); string strresponse = entityutils.tostring(response.getentity()); if(response.getstatusline().getstatuscode() == httpstatus.sc_ok) { return strresponse; } return strresponse; }catch(clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); }
i tried passing string ws_key , keyvalue , called url http://keyvalue@domain.com/api/namehere
but doesn't work too. please assist.
i know it's old question, think can help.
check .htaccess file. should has lines these:
rewriterule . - [e=rewritebase:/] rewriterule .* - [e=http_authorization:%{http:authorization}] rewriterule ^api$ api/ [l] rewriterule ^api/(.*)$ %{env:rewritebase}webservice/dispatcher.php?url=$1 [qsa,l]
the second line tell apache how process authorization.
Comments
Post a Comment