6

I have been trying since two days to set basic authentication from my android application to SharePoint 2013. I have used HttpUrlConnection,DefaultHttpClient,Retrofit and Volley but these all are showing Authorization failure error. Which is working fine in iOS application.Below is my Vollery code snippet.

private void sendJsonrequestSignIn(final String userName, final String password) {
    StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://192.168.50.31/sites/MobileDev/_vti_bin/listdata.svc/TestData",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.i("ResponseJson", response.toString());
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //Log.i("ErrorJson", error.getMessage());
                    Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
                }
            }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> params = new HashMap<String, String>();
            String creds = String.format("%s:%s", userName, password);
            String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.NO_WRAP);
            params.put("Authorization", auth);
            params.put("Accept", "application/json;odata=verbose");
            return params;
        }

    };
    requestQueue.add(stringRequest);
}
Satheesh
  • 1,722
  • 25
  • 35

3 Answers3

1

Have you tried Jshare. This library supports NTLM and works with Java and Android. I guess this might help with the NTLM authentication in your app.

Neji
  • 6,591
  • 5
  • 43
  • 66
1

You can use the Authenticator for basic-auth.

import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;


try {
    Authenticator.setDefault(new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("user", "password".toCharArray());
        }
    });
    URL url = new URL(DOWNLOAD_URL);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();

con.setReadTimeout(10000);
con.setConnectTimeout(15000);
con.setRequestMethod("GET");
con.setUseCaches(false);
con.connect();
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • I have tried your solution but not working, this also return 403 status code. Whether this problem occur from android or Sharepoint 2013. – Satheesh Sep 02 '16 at 07:46
0

I encountered this problem before and I fixed it.

Don't use

Basic Authentication

Use

NTLM Authentication

I just added a gist for this : https://gist.github.com/franciscerio/4b0a6a969eda3b93098a50174cffd8de#file-gistfile2-txt.

I forgot the original source that helped me. I hope you understand. :)

Fran Ceriu
  • 261
  • 2
  • 5