5

In my controller I am trying to call my API but I am unable to pass Autherization token.

I have created a config var and passing my token in it but getting following response:

900902Missing CredentialsRequired OAuth credentials not provided. Make sure your API invocation call has a header: "Authorization: Bearer ACCESS_TOKEN"

** JS Code **

 app.controller("AuthenticationController", function($scope, API_URL, vcRecaptchaService, $http) {

       var config = {
         headers: {
           'Authorization': 'Bearer 00000-e5673-346756f-8676-f7567561a'
         }
       };
       $scope.verifyRecaptcha = function() {

         if (vcRecaptchaService.getResponse() === "") {
           alert("User did not resolve the recaptcha")
         } else {
           var post_data = {
             'g-recaptcha-response': vcRecaptchaService.getResponse()
           }
           $http.post('https:1.1.1.1/abc/vdc/verify', config, post_data).success(function(response) {

               if (response.success === true) {
                 alert("Successfully resolved the recaptcha.");
               } else {
                 alert("User verification failed");
               }
             })
             .error(function(error) {
               alert("Error Occured while resolving recaptcha.");
               console.log(error);
             })
         }
       }
2

2 Answers 2

2

i think you've switched the position of the payload and the header

should be http.post(url, data, config) instead of http.post(url, config, data)

link to doc

I'd recommend to put all the logic inside an http interceptor, so the auth header will be appended to each request.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks bro. Banging head with walls.
1

To pass authorization in header, include $httpProvider in your angular module.

And the following function as factory :

function authInterceptor($localStorage) {

        return {
            request: function (config) {
                config.headers = config.headers || {};
                if ($localStorage.token) {
                    config.headers.Authorization = 'Bearer' + 
                    $localStorage.token;
                }
                return config;
            },
        };

    }

And call this factory in your module. So it will add Authorization header in all your $http requests automatically. And you don't need to add authorization header in each request.

2 Comments

I dont want to use local storage
OK. you can use another way instead of local storage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.