private APIAccessToken GetToken(String username, String password) { APIAccessToken accessToken = new APIAccessToken(); try { //Efetua o login var loginRequest = new { jsonrpc = "1.0", method = "user.login", parameters = new { user = username, password = password, userData = true //Deve retornar os dados para poder pegar o userID }, id = 1 }; JavaScriptSerializer _ser = new JavaScriptSerializer(); String jData = _ser.Serialize(loginRequest); if (jData == null) { throw new Exception("Username is empty"); } APIAuthResult ret = JSON.JsonWebRequest <APIAuthResult>(urlAPI, jData, "application/json", null, "POST"); if (ret == null) { accessToken.error = "Empty return"; Log(this, PluginLogType.Error, "Error on get API Auth 1.0 Token: " + accessToken.error); return(accessToken); } else if (ret.error != null) { accessToken.error = ret.error.data; Log(this, PluginLogType.Error, "Error on get API Auth 1.0 Token: " + accessToken.error); return(accessToken); } else if (!String.IsNullOrWhiteSpace(ret.result.sessionid)) { accessToken.access_token = ret.result.sessionid; accessToken.expires_in = ret.result.expires; accessToken.create_time = ret.result.create_time; accessToken.userid = ret.result.userid; } } catch (Exception ex) { Log(this, PluginLogType.Error, "Error on get API Auth 1.0 Token: " + ex.Message); return(null); } return(accessToken); }
private APIAccessToken GetToken(Dictionary <String, Object> config) { APIAccessToken accessToken = new APIAccessToken(); //accessToken.LoadFromFile(sufix); //Verifica em cache se o token ainda e válido //if (!accessToken.IsValid){ //accessToken = new APIAccessToken(); try { if (config == null) { throw new Exception("Config is empty"); } if (config["username"] == null) { throw new Exception("Username is empty"); } if (config["password"] == null) { throw new Exception("Username is empty"); } if (urlAPI == null) { throw new Exception("URI is empty"); } if (Service == null) { throw new Exception("Service is empty"); } String sufix = "-" + Service.Host + (Service.Port != 80 ? "-" + Service.Port : ""); //Efetua o login var loginRequest = new { jsonrpc = "1.0", method = "user.login", parameters = new { user = config["username"].ToString(), password = config["password"].ToString(), userData = false //Define se deseja ou não retornar os principais dados do usuário }, id = 1 }; JavaScriptSerializer _ser = new JavaScriptSerializer(); String jData = _ser.Serialize(loginRequest); if (jData == null) { throw new Exception("Username is empty"); } APIAuthResult ret = JSON.JsonWebRequest <APIAuthResult>(urlAPI, jData, "application/json", null, "POST"); if (ret == null) { accessToken.error = "Empty return"; if (Log != null) { Log(this, PluginLogType.Error, "Error on get API Auth 1.0 Token: " + accessToken.error); } return(accessToken); } else if (ret.error != null) { accessToken.error = ret.error.data; if (Log != null) { Log(this, PluginLogType.Error, "Error on get API Auth 1.0 Token: " + accessToken.error); } return(accessToken); } else if (!String.IsNullOrWhiteSpace(ret.result.sessionid)) { accessToken.access_token = ret.result.sessionid; accessToken.expires_in = ret.result.expires; accessToken.create_time = ret.result.create_time; try { accessToken.SaveToFile(sufix); } catch { } } } catch (Exception ex) { if (Log != null) { Log(this, PluginLogType.Error, "Error on get API Auth 1.0 Token: " + ex.Message); } accessToken.error = "Error on get API Auth 1.0 Token: " + ex.Message; } //} return(accessToken); }