GUISSOUMA Issam
GUISSOUMA Issam

Reputation: 2582

Authentication from NativeScript App(android/ios) to Sharepoint 2013 REST API

I'm trying to call Sharepoint 2013 REST API from an application developped with NativeScript(android/ios).

Using xhr or fetch NativeScript module I'm not able to authenticate correctly and call rest api.

Using Java I'm able to connect to the sharepoint server and call Rest API without problem Using this maven dependency: org.apache.httpcomponents httpclient 4.4.1.

public class SharePointClientAuthentication {

public static void main(String[] args) throws Exception {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
            new AuthScope(AuthScope.ANY),
            new NTCredentials("username", "password", "https://hostname", "domain"));
    CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider)
            .build();
    try {
        HttpGet httpget = new HttpGet("http://hostname/_api/web/lists");

        System.out.println("Executing request " + httpget.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            EntityUtils.consume(response.getEntity());
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}
}

Any one know what's the equivalent to the java code with NativeScript or a way to get Windows authentication(NTLM) to work.

Thanks in advance.

Upvotes: 1

Views: 839

Answers (1)

Hristo Deshev
Hristo Deshev

Reputation: 907

Initially I thought this should be handled by the native HTTP library and exposed as a part of the XMLHttpRequest implementation in NativeScript, but then I discovered ntlm.js. It's a small library that takes care of the NTLM challenge-response for you.

I patched it up a bit, to get rid of the browser dependency and push some polyfills, and got it running. I put up a demo project here:

https://github.com/hdeshev/nativescript-ntlm-demo

Upvotes: 1

Related Questions