6 Replies Latest reply on May 4, 2016 12:47 AM by Martin Seifert

    Pass authorization header to API call?

    Alec Barrett

      I'm trying to pass an Authorization token in the Authorization header of the API call I'm making from my WDC. I've verified that the call and token work via Postman. This is currently what my code looks like:

      var xhr = $.ajax({
           method: 'GET',
           url: apiUrl,
           dataType: 'json',
           headers: {
                'Authorization': accessToken
           },
           success: function(response){...},
           error: function(xhr,ajaxOptions,thrownError){...}
      })
      

      xhr.responseText is undefined and thrownError is blank, and no data is returned. Can anyone help me understand how to fix this?

        • 1. Re: Pass authorization header to API call?
          Jeff D

          Hi Alex, try running this in the simulator and use the browser debugging window (F12) to see what's being sent and also if there are errors on the console.

          2 of 2 people found this helpful
          • 2. Re: Pass authorization header to API call?
            Martin Seifert

            Hey Alec

             

            in general your AJAX call looks correct. And a blank error is also not necessary a bad thing. I see two possible issues:

             

            1. Since you didn't post how your "accessToken" object is generated, did you verify the authentication works correctly? Otherwise there would be an error usually... So that's probably not it.

             

            2. Did you verify your filters are valid? No data returned could imply you flipped the start- and end-date of your request since many APIs do not return an error in such cases.

             

            I struggled with these issues recently, too. Maybe you find the solution if you have a look at my working code and compare it to yours?! Here it is: Web Data Connector (WDC) for Webtrends

             

            Kind regards

            Martin

            2 of 2 people found this helpful
            • 3. Re: Pass authorization header to API call?
              Alec Barrett

              Thanks. This calls the error function (which currently just console.logs its arguments). So I know the call is failing, but the thrownError is just "error".

              • 4. Re: Pass authorization header to API call?
                Alec Barrett

                Hi Martin,

                 

                I have verified that the URL and token work via Postman.  I don't think I'm passing any filters -- this is my company's API, and it will just take simple URL parameters, which should be easy to figure out. In the mean time, I'm not including any parameters because I'm just trying to get the connection working.

                 

                Can you explain a little more about when/why you used a CORS proxy? It seems like it might solve my problem but I'd like to understand it a little more before sinking time into it.

                 

                Thanks,

                Alec

                • 5. Re: Pass authorization header to API call?
                  Alec Barrett

                  Good news -- I got my WDC working.  I successfully debugged it by stripping it down to just an ajax call, then building it back up to ensure I didn't have any unnecessary lines of code (which, it turns out, I did). Thanks again for the help.

                  • 6. Re: Pass authorization header to API call?
                    Martin Seifert

                    Hey Alec

                     

                    Great news! Would you mind posting the source of your issue? Just out of curiosity, maybe others can learn from your experience

                     

                    The CORS Proxy is used for cross-server communication. Usually servers (e.g. API servers) do not allow other servers to communicate with them for very good security reasons. So when you're firing a request to a server from another server the request is automatically denied. But with CORS there exists a protocol to enable two servers to talk to each other by restricting the allowed requests to a certain set of possible actions.

                     

                    The API I was addressing didn't allow open communication from another server, but it allowed CORS, so I use this proxy for my requests. That's all it does.

                     

                    Kind regards

                    Martin