internal void GetAccessAndRefreshToken(string successCode) { var client = new RestClient("https://live.european-language-grid.eu/auth/realms/ELG/protocol/openid-connect/token"); client.Timeout = -1; var request = new RestRequest(Method.POST); request.AddHeader("Content-Type", "application/x-www-form-urlencoded"); request.AddParameter("code", successCode); request.AddParameter("grant_type", "authorization_code"); request.AddParameter("client_id", "elg-oob"); request.AddParameter("redirect_uri", "urn:ietf:wg:oauth:2.0:oob"); //var response = client.Execute<ElgTokenResponse>(request); var response = client.Execute(request); if (response.IsSuccessful) { RestSharp.Serialization.Json.JsonDeserializer jsonDeserializer = new RestSharp.Serialization.Json.JsonDeserializer(); var tokenResponse = jsonDeserializer.Deserialize <ElgTokenResponse>(response); this.Status = ElgConnectionStatus.Ok; this.elgCreds.AccessToken = tokenResponse.access_token; this.elgCreds.RefreshToken = tokenResponse.refresh_token; } else { this.Status = ElgConnectionStatus.IncorrectSuccessCode; } //return response.Data; }
public void Login() { Client = new RestClient(LoginUrl); RestRequest request = new RestRequest(LoginUrl + "/services/oauth2/token", Method.POST); request.AddParameter("grant_type", "password"); request.AddParameter("client_id", ClientId); request.AddParameter("client_secret", ClientSecret); request.AddParameter("username", Username); request.AddParameter("password", Password); IRestResponse response = Client.Execute(request); if (HttpStatusCode.OK == response.StatusCode) { RestSharp.Serialization.Json.JsonDeserializer deserializer = new RestSharp.Serialization.Json.JsonDeserializer(); Dictionary <String, String> body = deserializer.Deserialize <Dictionary <String, String> >(response); Token = "Bearer " + body["access_token"]; InstanceUrl = body["instance_url"]; Client = new RestClient(InstanceUrl); return; } throw new AuthenticationException("Login error! Check provided login data."); }
private void RefreshAccessToken() { var client = new RestClient("https://live.european-language-grid.eu/auth/realms/ELG/protocol/openid-connect/token"); client.Timeout = -1; var request = new RestRequest(Method.POST); request.AddHeader("Content-Type", "application/x-www-form-urlencoded"); request.AddParameter("refresh_token", this.elgCreds.RefreshToken); request.AddParameter("grant_type", "refresh_token"); request.AddParameter("client_id", "elg-oob"); request.AddParameter("redirect_uri", "urn:ietf:wg:oauth:2.0:oob"); var response = client.Execute(request); if (response.StatusCode == HttpStatusCode.Unauthorized) { this.Status = ElgConnectionStatus.InvalidRefreshToken; } else { this.Status = ElgConnectionStatus.Ok; RestSharp.Serialization.Json.JsonDeserializer jsonDeserializer = new RestSharp.Serialization.Json.JsonDeserializer(); var tokenResponse = jsonDeserializer.Deserialize <ElgTokenResponse>(response); this.elgCreds.AccessToken = tokenResponse.access_token; } }
public async void flush() { if (body.records.Count == 0) { return; } String jsonBody = JsonConvert.SerializeObject(body, Formatting.None, new RecordObjectConverter()); body = new SalesforceBody(); String Url = InstanceUrl + "/services/data/" + ApiVersion + "/composite/tree/" + ParentObject; RestRequest request = new RestRequest(Url, Method.POST); request.AddHeader("Authorization", Token); request.AddHeader("Content-Type", "application/json"); request.AddJsonBody(jsonBody); Thread.Sleep(500); IRestResponse response = await Client.ExecutePostAsync(request); RestSharp.Serialization.Json.JsonDeserializer deserializer = new RestSharp.Serialization.Json.JsonDeserializer(); if (HttpStatusCode.Created == response.StatusCode) { SuccessResponse results = deserializer.Deserialize <SuccessResponse>(response); foreach (ResultSuccess result in results.results) { Logger.Info(String.Format("Object Reference: {0} added with id: {1}", result.referenceId, result.id)); } } else if (HttpStatusCode.BadRequest == response.StatusCode) { ErrorResponse errors = deserializer.Deserialize <ErrorResponse>(response); foreach (ResultError result in errors.results) { String message = ""; message = String.Format("Object Reference: {0} has errors: ", result.referenceId); foreach (Error error in result.errors) { message = message + error.message + " for fields ["; foreach (String errorMessage in error.fields) { message = message + errorMessage + ","; } } message = message.Substring(0, message.Length - 1) + "]"; Logger.Error(message); } } }
/// <summary> /// Deserialize a response and convert to a parametrized data /// </summary> /// <typeparam name="DTO"></typeparam> /// <param name="response"></param> /// <returns></returns> private DTO GetContent <DTO>(IRestResponse response) { try { var deserializer = new RestSharp.Serialization.Json.JsonDeserializer(); return(deserializer.Deserialize <DTO>(response)); } catch (Exception ex) { throw new Exception($"Failed to Deserialize a response. Error: {ex}"); } }
public void RetrieveMetadata(String ObjectName) { RestRequest request = new RestRequest(InstanceUrl + "/services/data/" + ApiVersion + "/sobjects/" + ObjectName + "/describe", Method.GET); request.AddHeader("Authorization", Token); IRestResponse response = Client.Execute(request); if (HttpStatusCode.OK == response.StatusCode) { RestSharp.Serialization.Json.JsonDeserializer deserializer = new RestSharp.Serialization.Json.JsonDeserializer(); Metadata desc = deserializer.Deserialize <Metadata>(response); Meta.Add(ObjectName, desc); return; } throw new ApplicationException("Error getting " + ObjectName + " Metadata"); }
public List <Sobject> RetrieveObjects() { RestRequest request = new RestRequest(InstanceUrl + "/services/data/" + ApiVersion + "/sobjects", Method.GET); request.AddHeader("Authorization", Token); IRestResponse response = Client.Execute(request); List <Sobject> objects = new List <Sobject>(); if (HttpStatusCode.OK == response.StatusCode) { RestSharp.Serialization.Json.JsonDeserializer deserializer = new RestSharp.Serialization.Json.JsonDeserializer(); //Console.WriteLine(response.Content); MetadataSobject sobjects = deserializer.Deserialize <MetadataSobject>(response); objects = sobjects.Sobjects; } return(objects); }
public void flush() { if (Payload.Count == 0) { return; } PrepareBody(); //Console.WriteLine("Flush salesforce data: {0}", body.records.Count); string jsonBody = JsonConvert.SerializeObject(body, Formatting.None, new RecordObjectConverter()); //Console.WriteLine("Salesforce payload: {0}", jsonBody); String Url = InstanceUrl + "/services/data/" + ApiVersion + "/composite/tree/" + ObjectName; //Console.WriteLine("Send data to Salesforce :" + Url); RestRequest request = new RestRequest(Url, Method.POST); request.AddHeader("Authorization", Token); request.AddHeader("Content-Type", "application/json"); request.AddJsonBody(jsonBody); IRestResponse response = Client.Execute(request); body = new SalesforceBody(); Payload = new List <ObjectPayload>(); //Console.WriteLine(response.Content); //Environment.Exit(0); RestSharp.Serialization.Json.JsonDeserializer deserializer = new RestSharp.Serialization.Json.JsonDeserializer(); if (HttpStatusCode.Created == response.StatusCode) { SuccessResponse results = deserializer.Deserialize <SuccessResponse>(response); foreach (ResultSuccess result in results.results) { Logger.Info(String.Format("Object Reference: {0} added with id: {1}", result.referenceId, result.id)); } } else if (HttpStatusCode.BadRequest == response.StatusCode) { ErrorResponse errors = deserializer.Deserialize <ErrorResponse>(response); String message = ""; foreach (ResultError result in errors.results) { message = String.Format("Object Reference: {0} has errors: ", result.referenceId); foreach (Error error in result.errors) { message = message + error.message + " for fiedds ["; foreach (String errorMessage in error.fields) { message = message + errorMessage + ","; } } message = message.Substring(0, message.Length - 1) + "]"; } Logger.Error(message); } }
public IRestResponse GetCollection() { Console.WriteLine("Get collection: {0}", Name); RestRequest request = new RestRequest("collections", Method.GET); request.AddHeader("Content-Type", "application/json"); Authorize(request); IRestResponse response = Client.Execute(request); RestSharp.Serialization.Json.JsonDeserializer deserializer = new RestSharp.Serialization.Json.JsonDeserializer(); CollectionList collections = deserializer.Deserialize <CollectionList>(response); foreach (CollectionInfo collection in collections.Collections) { if (collection.Name.Equals(Name)) { Id = collection.Id; Console.WriteLine("Found collection ID: {0}", Id); } } if (Id != null) { Console.WriteLine("Get collection by id: {0}", Id); request = new RestRequest("collections/{id}"); request.AddUrlSegment("id", Id); Authorize(request); response = Client.Execute(request); //parse environment if (Env != null && Env.Equals("") == false) { Console.WriteLine("Get Environment: " + Env); RestRequest envRequest = new RestRequest("environments"); Authorize(envRequest); IRestResponse envResponse = Client.Execute(envRequest); deserializer = new RestSharp.Serialization.Json.JsonDeserializer(); EnvList envs = deserializer.Deserialize <EnvList>(envResponse); String EnvId = ""; foreach (EnvInfo env in envs.Environments) { if (env.Name.Equals(Env)) { EnvId = env.Id; Console.WriteLine("Found environment ID: {0}", EnvId); break; } } if (!EnvId.Equals("")) { envRequest = new RestRequest("environments/{id}"); envRequest.AddUrlSegment("id", EnvId); Authorize(envRequest); envResponse = Client.Execute(envRequest); EnvironmentBody envBody = deserializer.Deserialize <EnvironmentBody>(envResponse); foreach (Value value in envBody.Environment.Values) { if (!BlackList.Contains(value.Key)) { Console.WriteLine("Parse environment value: " + value.Key + " [" + value.value + "]"); response.Content = response.Content.Replace("{{" + value.Key + "}}", value.value); } } } } CollecitonDetail detail = deserializer.Deserialize <CollecitonDetail>(response); Console.WriteLine("Number of collections' endpoints: {0}", detail.Collection.Item.Count); Collection = detail.Collection; } else { String message = "Collection " + Name + " not found!!!"; throw new Exception(message); } return(response); }