Apachai.9475:

I am trying to access the characters api using angularJS and passing my api key inside the http header, but the server returns an error code -1 without a text.

If I try to use the api by passing the token in the url I get the response I need, but I’d rather try to use it through headers.

My code is this

charApp.factory(‘CharactersHeaders’, function($resource) {
return $resource(“https://api.guildwars2.com/v2/characters?page=0”, null ,
{
“query”: {method: “GET”,headers: { “Authorization”: “Bearer <<API KEY>>”}}
}
);
});

I made a bare bones rails api to test my javascript and I can see that the header is present on the request.

Not sure what I’m doing wrong.
I had made a java web app to get the data, and was able to connect and retrieve data using headers, but I can’t get it to work using angularJs

Can anyone tell me the correct way?

Lawton Campbell.8517:

It’s a CORS issue — setting an Authorization header (or User-Agent for that matter) makes the API request a non-simple request. This means the browser attempts to preflight it with an OPTIONS request, which our backend doesn’t support, so the request fails. So for code running in the browser, you’re stuck with the access_token query parameter.

Apachai.9475:

Thank you, I was running out of ideas to make this work.