/// <summary> /// Initializes a new instance of the <see cref="SalesforceClient"/> class. /// </summary> /// <param name="restClient">The rest client.</param> protected internal SalesforceClient(IRestClient restClient) { m_restClient = restClient; ApiVersion = "v28.0"; m_deserializer = new DynamicJsonDeserializer(); genericJsonDeserializer = new GenericJsonDeserializer(new SalesforceContractResolver(false)); updateJsonSerializer = new GenericJsonSerializer(new SalesforceContractResolver(true)); }
/// <summary> /// Authenticate in the Salesforce REST's API. /// </summary> /// <returns> /// The authentication info with access token and instance url for futher API calls. /// </returns> /// <remarks> /// If authentiaction fails an SalesforceException will be throw. /// </remarks> public AuthenticationInfo Authenticate() { if (ServicePointManager.SecurityProtocol != 0) { ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12; } Uri uri = new Uri(TokenRequestEndpointUrl); m_restClient.BaseUrl = uri; var request = new RestRequest(Method.POST) { RequestFormat = DataFormat.Json }; request.AddParameter("grant_type", "password"); request.AddParameter("client_id", m_clientId); request.AddParameter("client_secret", m_clientSecret); request.AddParameter("username", m_username); request.AddParameter("password", m_password); var response = m_restClient.Post(request); var isAuthenticated = response.StatusCode == HttpStatusCode.OK; var deserializer = new GenericJsonDeserializer(new SalesforceContractResolver(false)); var responseData = deserializer.Deserialize <dynamic>(response); if (responseData == null) { throw new SalesforceException(response.ErrorException.Message, response.ErrorMessage); } if (isAuthenticated) { return(new AuthenticationInfo(responseData.access_token.Value, responseData.instance_url.Value)); } else { throw new SalesforceException(responseData.error.Value, responseData.error_description.Value); } }