Forum for new developers

Forum for new developers

in API Development

Posted by: RobbyT.1925

RobbyT.1925

I figured I’d start a forum for new developers who are having specific issues with the v2 API. This is not for general programming questions, it’s specifically for getting help with issues that have something to do with using the API. To get started, here’s an issue I’m having.

I’m trying to return the array of characters on my account, yet all I’m getting is the array of API versions. Can anyone explain where I’m going wrong and how to fix it?

window.onload = function(){
	function createCORSRequest(method, url){
		var request = new XMLHttpRequest();
		if("withCredentials" in request){
			request.open(method, url, true);
		}
		else if(typeof XDomainRequest != "undefined"){
			request = newXDomainRequest();
			request.open(method, url);
		}
		else{
			request = null;
		}
		return request;
	}
	
	function makeCORSRequest(){
		var key = API Key;
		var url = "https://api.guildwars2.com?access-token=<" +key +">/v2/account";
		//var url = "https://api.guildwars2.com";
		var request = createCORSRequest("GET", url);
		if(!request){
			alert("CORS not supported");
			return;
		}
		
		request.onload = function(){
			var text = request.responseText;
			document.write(text);
			alert(text);
		}
		
		request.onerror = function(){
			alert("Error");
		}
		
		//request.setRequestHeader("Authorization", "Bearer<68ADC137-8DB1-904C-BEA3-7F4F09458405109C9565-8B0F-43B6-9154-A3C1BF80962C>");
		request.send();
	}
	
	makeCORSRequest();
}

(edited by RobbyT.1925)

Forum for new developers

in API Development

Posted by: Lawton Campbell

Lawton Campbell

Web Programmer

Next

We worked this out over gitter, but for anyone else following along, the issue was that

var url = “https://api.guildwars2.com?access-token=<” +key +">/v2/account";

should have been

var url = “https://api.guildwars2.com/v2/account?access_token=” + key;

Forum for new developers

in API Development

Posted by: Aidan Savage.2078

Aidan Savage.2078

Also lawton, wouldnt the requestheader call-

}
		
		//request.setRequestHeader("Authorization", "Bearer&lt;<snipped because OP probably doesnt want it shared>&gt;");
		request.send();
	}

be slightly redundant, as that’s in effect what you’re doing when you append the apikey to the end of the url?

Forum for new developers

in API Development

Posted by: Lawton Campbell

Previous

Lawton Campbell

Web Programmer

Yeah, it would.

Our backend doesn’t support OPTIONS requests which means we can’t preflight CORS requests — so code running in the browser can’t actually set an authentication header — so for most use-cases the key has to go in the URL. Which is a bit unfortunate, but it is what it is.