I would like to do a token request from angular like below:
http --auth <email>:<password> --json GET http://127.0.0.1:5000/api/v1.0/token
the above is httpie, description below:
--auth, -a Pass a username:password pair as the argument. Or, if you only specify a username (-a username), you’ll be prompted for the password before the request is sent. To send a an empty password, pass username:. The username:password@hostname URL syntax is supported as well (but credentials passed via -a have higher priority). --auth-type Specify the auth mechanism. Possible values are basic and digest. The default value is basic so it can often be omitted.
here is the output of the above command:
root@localhost:/var/www/myapp# http --auth myname@email.com:thepassword --json GET http://127.0.0.1:5000/api/v1.0/token
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 159
Content-Type: application/json
Date: Wed, 25 Nov 2015 03:13:16 GMT
Server: nginx/1.4.6 (Ubuntu)
Set-Cookie:session=eyJfaWQiOnsiIGIiOiJOR000TkdGak1UQTFabUppTldNeE1HVTBZemt3TjJWaU1EVmpNemN4WkdZPSJ9fQ.CTa4zA.YoXlQf5H68q3lRzuAEmEQ5kRm_I; HttpOnly; Path=/
{
"expiration": 0,
"token": "eyJhbGciOiJIUzI1NiIsImV4cCI6MTQ0ODQyNDc5NiwiaWF0IjoxNDQ4NDIxMTk2fQ.eyJpZCI6MX0.NY5hSBVjeddctBVvlM1cDJw2A9Yv38om5cqyBQUQhgU"
}
below is the code i have on angularjs:
$scope.login = function() {
var config = {
method: 'GET',
url: '/api/v1.0/token',
headers: {'auth':'myname@email.com:thepassword'}
};
$http(config)
.success(function(data, status, headers, config) {
if (data.status) {
//logged in
}
else {
//failed
}
})
.error(function(data, status, headers, config) {
//failed
});
}
i hard-coded with the email and password to the code, so when i click on the button, it should use the email/password to get the token from the server with the correct output like above, but instead, i got this:
Failed to load resource: the server responded with a status of 401 (UNAUTHORIZED)
I don't know what went wrong, please correct me if i missed anything.
Thanks.