Authenticate to Sharepoint online via Rest api + Angularjs

Asked

Viewed 192 times

3

I’m trying to create an application using Visual Studio 2015, Cordova + Angularjs.

My need today is to authenticate in Sharepoint and consume Web Services. I’ve tried several authentication attempts, but I’m not succeeding.

(function () {

var app = angular.module('spContact', ['ngRoute']);

app.factory('spAuthService', function ($http, $q) {

    var authenticate = function (userId, password, url) {

        var signInurl = 'https://' + url + '/_forms/default.aspx?wa=wsignin1.0';
        var deferred = $q.defer();
        var message = getSAMLRequest(userId, password, signInurl);

        $http({
            method: 'POST',
            url: 'https://login.microsoftonline.com/extSTS.srf',
            data: message,
            headers: {
                'Content-Type': "text/xml; charset=\"utf-8\""
            }
        }).success(function (data) {
            getBearerToken(data, signInurl).then(function (data) {
                deferred.resolve(data);
            }, function (data) {
                deferred.reject(data)
            })
        });

        return deferred.promise;
    };

    return {
        authenticate: authenticate
    };

    function getSAMLRequest(userID, password, url) {
        return 'envelope';
    }

    function getBearerToken(result, url) {

        var deferred = $q.defer();
        var securityToken = $($.parseXML(result)).find("BinarySecurityToken").text();
        if (securityToken.length == 0) {
            deferred.reject();
        }
        else {
            $http({
                method: 'POST',
                url: url,
                data: securityToken,
                headers: {
                    Accept: "application/json;odata=verbose"
                }
            }).success(function (data) {
                deferred.resolve(data);
            }).error(function () {
                deferred.reject();
            });
        }

        return deferred.promise;
    }
});

})();

Someone can help me?

1 answer

0


It was a problem of cross-Domain (login.microsoftonline.com is an external domain, of course). More details on how to solve this type of problem in Phonegap Enable CORS via Javascript (Phonegap).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.