mark1234
mark1234

Reputation: 1121

Angular Digest Authentication

I'm trying to connect to a RESTFul API on a home automation server in my house (so massive security not required). When I try to connect with no credentials, I get:

[Error] XMLHttpRequest cannot load http://localhost:8176/devices/. Origin http://localhost is not allowed by Access-Control-Allow-Origin. 

The documentation says:

Indigo uses "digest authentificaction" (not basic nor token)

curl -X PUT --digest -u {username}:{password} -d value={value} http://{IPAddress}:8176/variables/{VariableToChange}

curl -X PUT --digest -u admin:p@ssw0rd -d value=true http://{IPAddress}:8176/variables/Var1

To get a list of devices, I can use one of these 3:

http://127.0.0.1:8176/devices/
http://127.0.0.1:8176/devices.xml/
http://127.0.0.1:8176/devices.txt/

I'm at a total loss on how to pass the creeds. Here's my code so far:

 function DeviceController($scope, $http) {
    $http.get("http://localhost:8176/devices/")
        .then(function (results) {
            //Success;
            console.log("Succss: " + results.status);
            $scope.devices = results.data;
        }, function (results) {
            //error
            console.log("Error: " + results.data + "; "
                                  + results.status);
            $scope.error = results.data;
        })
};

Can anyone point me in right direction?

Thanks

Mark

Upvotes: 3

Views: 4085

Answers (1)

gtramontina
gtramontina

Reputation: 1136

Your server has to be configured to accept CORS (Cross-origin resource sharing).

EDIT: It looks like your Indigo (don't know what it is) server is not the one serving your angular app. Please refer to your server's documentation on how to enable CORS.

As far as passing your credentials during your request, do:

$http({
    method: "GET",
    url: "http://localhost:8176/devices/",
    headers: {"Authorization": "Basic " + btoa("admin:p@ssw0rd")},
})...

Upvotes: 2

Related Questions