无法将数据发布到UAT服务器中的Salesforce

时间:2019-09-17 11:04:48

标签: c# api salesforce

我创建了一个api请求,用于发布表单数据以在Salesforce中创建销售线索。

首先,我请求授权密钥以将数据插入Salesforce (使用oauth,客户端ID和客户端密码。)

这在本地有效,数据已成功发布到salesforce中。 接下来,我将此代码移至UAT服务器。在该UAT服务器中,我无法获取授权密钥,并且数据未发布到salesforce

下面我发现了LOG文件中的错误

 //Function to Get Authorization
        public static string GetAuthorizationKey(ApiAuthorizeModel apiAuthorizeModel)
        {
            try
            {
                string jsonResponse = string.Empty;
                string AuthToken = string.Empty;
                string Error = string.Empty;
                string ErrorDescription = string.Empty;

                if (!string.IsNullOrEmpty(apiAuthorizeModel.ClientID) && !string.IsNullOrEmpty(apiAuthorizeModel.ClientSecret) && !string.IsNullOrEmpty(apiAuthorizeModel.UserName) && !string.IsNullOrEmpty(apiAuthorizeModel.Password) && !string.IsNullOrEmpty(apiAuthorizeModel.TokenURI))
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
                    using (var client = new HttpClient())
                    {
                        var request = new FormUrlEncodedContent(new Dictionary<string, string>{
                {ApiConstants.Grant_type, ApiConstants.Password},
                {ApiConstants.Client_key,apiAuthorizeModel.ClientID},
                {ApiConstants.Client_secret, apiAuthorizeModel.ClientSecret},
                {ApiConstants.Username, apiAuthorizeModel.UserName},
                {ApiConstants.Password, apiAuthorizeModel.Password}
                });

                        request.Headers.Add("X-PrettyPrint", "1");
                        var response = client.PostAsync(apiAuthorizeModel.TokenURI, request).Result;
                        jsonResponse = response.Content.ReadAsStringAsync().Result;
                    }
                    JObject jObject = JObject.Parse(jsonResponse);
                    Error = (string)jObject[ApiConstants.error];
                    ErrorDescription = (string)jObject[ApiConstants.error_description];
                    if (string.IsNullOrEmpty(Error))
                    {
                        AuthToken = (string)jObject[ApiConstants.AccessToken];
                    }
                    else
                    {
                        Log.Error(Error, typeof(SendDataToApi));
                        Log.Error(ErrorDescription, typeof(SendDataToApi));
                        isAPIErrorFlag = true;
                    }
                }
                return AuthToken;
            }
            catch (Exception e)
            {
                Log.Error("Method : GetAuthorizationKey :", e, typeof(SendDataToApi));
                Log.Error(string.Concat("GetAuthorizationKey: ", e.Message.ToString()), typeof(SendDataToApi));
                return null;
            }
        }
  

2656 18:37:22 INFO方法:GetAuthorizationKey   :System.AggregateException:发生一个或多个错误。 ->   System.Net.Http.HttpRequestException:发送时发生错误   请求。 ---> System.Net.WebException:无法连接到   远程服务器---> System.Net.Sockets.SocketException:连接   尝试失败,因为关联方未正确响应   一段时间后,或建立的连接失败,原因是   连接的主机在以下位置无法响应101.53.162.131:443   System.Net.Sockets.Socket.InternalEndConnect(IAsyncResult asyncResult)   在System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
  在System.Net.ServicePoint.ConnectSocketInternal(Boolean   connectFailure,Socket s4,Socket s6,Socket&套接字,IPAddress&   地址,ConnectSocketState状态,IAsyncResult asyncResult,   异常&异常)---内部异常堆栈跟踪的结尾---
  在System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult   asyncResult,TransportContext&context)   System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult   ar)

1 个答案:

答案 0 :(得分:0)

感谢您提出的所有宝贵意见。我已使用此信息在您的CRM服务器和代理服务器之间建立连接。

根据您的环境,将服务器配置为允许在machine.config或服务器的根web.config(IIS级别)上通过代理进行通信。

<system.net> 
    <defaultProxy> 
            <proxy usesystemdefault = "false" proxyaddress="http://ip address:port" bypassonlocal="false"/> 
    </defaultProxy> 
</system.net>

它解决了我的问题。 我引用的以下网站

https://help.salesforce.com/articleView?id=mc_mdc_crm_server_proxy_configuration.htm&type=5