I have been trying all sorts of things in different order but cannot ever get a successful response.
Here is simplified dumbed down version of what I’m doing :
public function login(username:String, password:String)
{
var url = "https://myAPIURI.herokuapp.com/auth/issue-token";
var request:URLRequest = new URLRequest(url);
var variables = {"appId":"myAppID", "apiKey":"myAPIKey", "user":username, "pass":password };
var urlVariables = new URLVariables(haxe.Json.stringify(variables));
request.method = URLRequestMethod.POST;
request.data = urlVariables;
request.contentType = "application/json";
var loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, successCallbackMethod);
loader.addEventListener(ErrorEvent.ERROR, errorCallbackMethod);
loader.addEventListener(IOErrorEvent.IO_ERROR, IOErrorCallbackMethod);
loader.load(request);
}
And then one would simply call the login method like this:
I am trying to get this to work on all platforms starting with IOS, Android and Mac.
How on earth is this suppose to work ? Am I on the right track ? I’ve been at this for way too long.
The first thing you could try is to set the “data” value as a String, directly. Use what you have already, set that as the “data” and see what happens.
Another thing I might try is to use URLVariables, like “var urlVariables = new URLVariables ()” then set the .appId to “myAppID”, etc, but perhaps this would not be as good because you definitely want it to be JSON. If the above does not work, also (maybe?) try adding the “Content-Type” header, manually.
For clarity’s sake and for folks struggling with calling a OAuth style restful API built with express, passport and nodejs, here is how I’ve finally connected to the API with an openFL built UI.
FIrst in, authentication:
private var authToken:String;
public function login(username:String, password:String)
{
var url = "https://myAPIURI.herokuapp.com/auth/issue-token";
var request:URLRequest = new URLRequest(url);
// Passing data as a string is your best bet here, also use haxe's String Interpolation and DO NOT waste time building your string by concatenating values as the interpreted value will surely hold lost quotes that will break your call.
var variables = '{ "appId": "$applicationID", "apiKey": "$apiKey", "user": "$username", "pass": "$password" }';
request.method = URLRequestMethod.POST;
request.data = urlVariables;
request.contentType = "application/json";
var loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, function(evt)
{
var data = haxe.Json.parse(evt.target.data);
// grab your auth token from the data, the key name for this might change depending on your architecture.
authToken = data.token;
getMyAppData();
});
loader.addEventListener(ErrorEvent.ERROR, function(err) { trace("Error"); } );
loader.addEventListener(IOErrorEvent.IO_ERROR, function(err) { trace("IOError"); } );
loader.load(request);
}
Now add whatever method (in this case getMyAppData):
function getMyAppData(?vars)
{
var request:URLRequest = new URLRequest(https://myAPIURI.herokuapp.com/MyURLMethodRoute);
request.requestHeaders = [new URLRequestHeader("Authorization", "Bearer "+authToken) ];
request.method = URLRequestMethod.GET;
request.contentType = "application/json";
if(vars != null)
request.data = vars;
var loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, function(evt)
{
var data = haxe.Json.parse(evt.target.data);
trace("SUCCESS ");
});
loader.load(request);
}
Edit: My wifi wasn’t connected on my device >.> Works perfectly on Android as well
This kind of URLRequest thing works no problem on Mac, Flash and iOS. I can’t though seem to get it to work on Android. Keeps popping up an IO_Error. Should I be including a permission in the AndroidManifest file or…?