public static T GetAuthentication <T>(string baseUrl, string url, Method httpMethod , string userName, string password, OAuthHeader oAuthHeader = null) { var client = new RestClient(baseUrl); var request = new RestRequest(url, Method.POST); string encodedBody = string.Format("username={0}&password={1}&grant_type={2}", userName, password, "password"); request.AddParameter("application/x-www-form-urlencoded", encodedBody, ParameterType.RequestBody); request.AddParameter("Content-Type", "application/x-www-form-urlencoded", ParameterType.HttpHeader); var response = client.Execute(request); return(JsonConvert.DeserializeObject <T>(response.Content)); }
public void SetAuthHeader(string token) { ApiHeader = new OAuthHeader(token); }
protected BaseApiConsumer(string token) { ApiHeader = new OAuthHeader(token); }
public static T InvokeApi <T>(string baseUrl, string url, Method httpMethod, OAuthHeader oAuthHeader = null, object body = null) { return(InvokeApiAsync <T>(baseUrl, url, httpMethod, oAuthHeader, body)); }
public static T InvokeApiAsync <T>(string baseUrl, string url, Method httpMethod, OAuthHeader oAuthHeader = null, object body = null) { var client = new RestClient(baseUrl); var request = new RestRequest(url, httpMethod); ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); }; if (oAuthHeader != null) { foreach (var header in oAuthHeader.GetHeaders()) { request.AddHeader(header.Key, header.Value); } } else { var userCredentailsKeyValue = body as ApiTokenObject; if (userCredentailsKeyValue != null) { client.Authenticator = new HttpBasicAuthenticator(userCredentailsKeyValue.username, userCredentailsKeyValue.password); } } if (SessionHelper.UserEntity != null) { request.AddHeader("Id_User", SessionHelper.UserEntity.Id_User.ToString()); } if ((httpMethod == Method.POST || httpMethod == Method.PUT || httpMethod == Method.DELETE) && body != null) { request.AddParameter("Content-Type", "application/x-www-form-urlencoded", ParameterType.HttpHeader); request.AddParameter("application/json", JsonConvert.SerializeObject(body), ParameterType.RequestBody); //request.AddBody(body); } var response = client.Execute(request); return(JsonConvert.DeserializeObject <T>(response.Content)); }