Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 537 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
401 Error when connecting to PrestaShop webservice from android

#1
I am trying to call a webservice in PrestaShop but i get 401 not authorized error. Even though i have passed the username key. I tried the authenticator too but i get an error HttpRetryingError.

Find below the code snippet of what i have done.

**Method one:**

final String username = "key_here";
final String password = "";// leave it 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 your code
urlConnection.setRequestProperty("Authorization","Basic " + authBytesString);

int statusCode = urlConnection.getResponseCode(); ---> i get 401
if (statusCode != HttpURLConnection.HTTP_OK) {
// throw some exception
InputStream in =
new BufferedInputStream(urlConnection.getInputStream());
Log.e("tag", getResponseText(in));
}


**Method two**

final String username = "keyvaluehere";
final String password = "";// leave it 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 the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 30000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for 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 even tried passing the string as ws_key and the keyvalue and i even called the url as

[To see links please register here]


but it doesn't work too.
Please assist.
Reply

#2
I know it's an old question, but I think I can help.

Check .htaccess file. It should has lines like 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 to process the authorization.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through