0

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.

triston
  • 493
  • 1
  • 10
  • 24
  • 2
    What does not working mean? Are you receiving an error, if so, go ahead and post it. – lux Nov 25 '15 at 00:55
  • What is that `http` command at the top of your question? What do the `--auth` and `--json` options do? – Phil Nov 25 '15 at 01:01
  • 1
    Possible duplicate of [How do I get basic auth working in angularjs?](http://stackoverflow.com/questions/17959563/how-do-i-get-basic-auth-working-in-angularjs) – Phil Nov 25 '15 at 01:08
  • please take a look at the question again, i added extra description about how the http command works. I just don't get how i can get the "--auth : " part to the request, and the --json means it expect a json response. – triston Nov 25 '15 at 03:34
  • @lux there is no error, it return "unauthorized", i know i put the email and password correct, i can run the http command with the same email and password, it worked. But i just can't get the angular part works. extra output posted. – triston Nov 25 '15 at 03:41
  • @triston, why did you use `GET`? You can use `POST` request for authentication. – Abhilash Augustine Nov 25 '15 at 03:49

0 Answers0