Exemple #1
0
        public RESTAPIClient(string consumerKey, string consumerSecret, string userName, string password, string token,
                             string baselineURL, string authEndpoint, string customURL)
        {
            _consumerKey    = consumerKey;
            _consumerSecret = consumerSecret;
            _userName       = userName;
            _password       = password;
            _token          = token;
            _baselineURL    = baselineURL;
            _authEndpoint   = authEndpoint;
            _customURL      = customURL;

            _authResponse = new APIResponse_Auth();
            _isAuthen     = false;
        }
Exemple #2
0
        private async Task Authenticate()
        {
            try
            {
                using (var client = new HttpClient())
                {
                    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11;
                    // TODO - Send HTTP requests
                    // set the base URI for HTTP requests...
                    client.BaseAddress = new Uri(_authEndpoint);
                    // set the Accept header to "application/json", which tells the server to send data in JSON format...
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    // HTTP POST
                    #region "Bug Example"
                    //var authen = new Authen() { grant_type = "password", client_id = AppSettings.salesforce_consumerKey,
                    //    client_secret = AppSettings.salesforce_consumerSecret,
                    //    user_name = AppSettings.salesforce_userName, password = AppSettings.salesforce_password + AppSettings.salesforce_token };
                    //HttpResponseMessage response = await client.PostAsJsonAsync("", authen);
                    // NOTE: the call to HttpClient.PostAsJsonAsync gave this error:
                    // {"error":"unsupported_grant_type","error_description":"grant type not supported"}
                    // This API expects form-urlencoded, not JSON on the request body.

                    // This also donesn't work.
                    //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
                    //client.DefaultRequestHeaders.Add("Content-Type", "application/x-www-form-urlencoded");
                    #endregion

                    // build content with "application/x-www-form-urlencoded" content type...
                    Dictionary <string, string> dictForm = new Dictionary <string, string>();
                    dictForm.Add("grant_type", "password");
                    dictForm.Add("client_id", _consumerKey);
                    dictForm.Add("client_secret", _consumerSecret);
                    dictForm.Add("username", _userName);
                    dictForm.Add("password", _password + _token);
                    var content = new FormUrlEncodedContent(dictForm);
                    //var content = dictForm;

                    // send POST method...
                    HttpResponseMessage response = await client.PostAsync(_authEndpoint, content).ConfigureAwait(false);

                    _authResponse.HTTPStatusCode = Convert.ToString(response.StatusCode);

                    if (response.IsSuccessStatusCode)
                    {
                        _authResponse = await response.Content.ReadAsAsync <APIResponse_Auth>();

                        if (_authResponse == null)
                        {
                            //throw new Exception("_authenResult is null...");
                        }

                        _isAuthen = true;
                    }
                    else
                    {
                        // get response content, which contains potential error messages...
                        _isAuthen             = false;
                        _authResponse.Content = await response.Content.ReadAsStringAsync();
                    }
                } // end using...
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }