1

I've read a lot of the other questions, but none have helped, so I thought I'd ask about my specific situation.

I'm writing a dashboard using AngularJS to draw data from several of our in house tools (Artifactory, Jenkins etc.). A lot of the various end points require authentication, but I've never done this with AngularJS.

How, using the $http service, can I set the username and password on the requests? I don't think I can set defaults using

$httpProvider.defaults.headers.common

because I'm using multiple APIs with different credentials.

EDIT: As someone mentioned it's similarity to another question, I have followed the instructions from that question, and I just keep getting 'OPTIONS 401 NOT AUTHORISED' in the Chrome console. I suppose the obvious answer to that is that I'm using the wrong password, but I'm not.

Will
  • 1,568
  • 2
  • 12
  • 28
  • How about using Token Authentication?. So first you have your own custom Login with Back-end setup which Authenticate your user and generate the Token. After that,using generated token you authenticate with the different API calls. Is the call you making is the 3rd party API's or in-house? – GeekOnGadgets Nov 18 '14 at 23:10
  • The APIs I'm calling are in house, but on a different domain (so CORS is also an issue). – Will Nov 18 '14 at 23:22
  • I suggest you should use `$http interceptors`. Then you can check where the request is going and set appropriate credentials if needed. – Michał Miszczyszyn Nov 18 '14 at 23:28
  • How would I do this? – Will Nov 18 '14 at 23:29
  • @Miszy, Please correct me if I am wrong, OP needs to authenticate first to get access to API and all have different credentials and $http interceptors will setup a API-Token for any One which is Authenticated and not all. – GeekOnGadgets Nov 18 '14 at 23:42
  • possible duplicate of [AngularJS - set HTTP header for one request](http://stackoverflow.com/questions/11876777/angularjs-set-http-header-for-one-request) – Ed_ Nov 19 '14 at 00:15
  • @will are you sending back the token to API with your call?.What is your header sending back? try using firebug in firefox and it will give you proper error msg – GeekOnGadgets Nov 19 '14 at 04:12
  • @GeekOnGadgets Sorry, I don't understand. Sending back the token? What's the token? When trying to authenticate against Artifactory, the response has a header: WWW-Authenticate:Basic realm="Artifactory Realm" Sounds like it might be important... – Will Nov 19 '14 at 04:47
  • @GeekOnGadgets What do you mean? OP needs to authenticate and this can be done by setting default headers or using interceptors. – Michał Miszczyszyn Nov 19 '14 at 08:59

1 Answers1

0

You can set the headers per request in the config (see https://docs.angularjs.org/api/ng/service/$http#usage)

Example

var options = {
    url: 'https://cors-test.appspot.com/test',
    headers: {
        Foo: 'Bar'
  }
};
$http(options);