Hi Scott,
It happens using JsonServiceClient, I am adding extra headers but I am not clearing them so that can't be the problem and I have a general routine to login as shown below, you can see where I tried the API key and then commented it out:
- Code: Select all
public static JsonServiceClient GetAPIClient(string URL = "", bool LoggedIn = true)
{
string methodName = MethodBase.GetCurrentMethod().Name;
string className = "ERP";
ERP.LogString("User: " + ERP.GetUser().UserLogString(), "Class : " + className + " Method: " + methodName);
try
{
// JsConfig.IncludeNullValues = true;
// ServiceStack.ClientConfig.SkipEmptyArrays
if (URL == "")
{
URL = ConfigManager().GetString("JiwaAPIURL");
}
JsonServiceClient client = new JsonServiceClient(URL);
client.Headers.Add("jiwa-stateful", "false");
client.Timeout = new TimeSpan(0, ERP.ConfigManager().GetInt("APITimeOut"), 0);
client.ReadWriteTimeout = new TimeSpan(0, ERP.ConfigManager().GetInt("APITimeOut"), 0);
//var cookie1 = new Cookie("USERDETAILS", GetUser().UserLogString());
client.SetCookie("USERDETAILS", GetUser().UserSSString());
// client.CookieContainer.Add(cookie1);
if (bool.Parse(ConfigManager().GetString("LogRequests")))
{
client.RequestFilter = request =>
{
//request.GetRequestStream
//string dstring = request.Dump();
//string json = request.ToSafeJson();
//string strreq = request.SerializeToString();
string strMethod = request.Method;
string straddress = request.Address.ToString();
ERP.LogString("Request ", "Method : " + strMethod + " Address: " + straddress);
};
}
//{
// //string strresp = response.SerializeToString();
// //using (var stream = request.GetRequestStream())
// //using (var sr = new StreamReader(stream))
// //{
// // var text = sr.ReadToEnd();
// //}
//};
if (bool.Parse(ConfigManager().GetString("LogResponses")))
{
client.ResponseFilter = response =>
{
string strStatusCode = response.StatusDescription;
//string strresp = response.SerializeToString();
string straddress = response.ResponseUri.ToString();
ERP.LogString("Response ", "Status Code : " + strStatusCode + " Address: " + straddress);
//using (var stream = response.GetResponseStream())
//using (var sr = new StreamReader(stream))
//{
// var text = sr.ReadToEnd();
//}
};
}
//client.Headers.Add("Authorization", "BearerAPI KEy goes here");
//return client;
if (LoggedIn)
{
var authResponse = client.Send<ServiceStack.AuthenticateResponse>(new ServiceStack.Authenticate()
{
provider = "credentials",
UserName = ConfigManager().GetString("JiwaAPIUser"),
Password = ConfigManager().GetString("JiwaAPIPassword"),
RememberMe = true
});
}
return client;
}
catch (Exception ex)
{
string error = ex.Message;
return null;
}
}
and the routine that calls the request is
- Code: Select all
public static List<Note> GetJiwaAPIDebtorNotes(JsonServiceClient inClient = null, string DebtorID = "")
{
string methodName = MethodBase.GetCurrentMethod().Name;
string className = "ERP";
ERP.LogString("User: " + ERP.GetUser().UserLogString(), "Class : " + className + " Method: " + methodName);
List<Note> result = new List<Note>();
DebtorNotesGETManyRequest req = new DebtorNotesGETManyRequest();
try
{
User user = GetUser();
if (DebtorID == "")
{
DebtorID = user.DebtorID;
}
req.DebtorID = DebtorID;
JsonServiceClient client;
bool nullclient = inClient == null;
if (nullclient)
{
client = GetAPIClient();
}
else
{
client = inClient;
}
result = client.Get<List<Note>>(req);
if (nullclient)
{
Logout(client);
}
}
catch (Exception ex)
{
result = new List<Note>();
}
return result;
}
I can't actually send you the project as its fairly massive and is an upgrade of a current production system that was written about fifteen years ago. By the way GetAPIClient works with the API Key for other request just not the notes get many.