可以';t连接到C#中的SalesForce

本文关键字:中的 SalesForce 连接 可以 | 更新日期: 2023-09-27 18:25:15

我正在学习本教程http://wiki.developerforce.com/page/Integrating_Force.com_with_Microsoft_.NET

然而,我得到了这个错误:

LOGIN_MUST_USE_SECURITY_TOKEN:用户名、密码和安全性无效代币或者用户被锁定。你在新的地方吗?访问时Salesforce——通过桌面客户端或API——从外部您公司的受信任网络,您必须将安全令牌添加到登录密码。若要接收新的安全令牌,请登录到salesforce.comhttp://login.salesforce.com然后单击"设置"|"我的"个人信息|重置安全令牌。

这是我控制台应用程序中的代码:

static void Main(string[] args)
        {
            string userName;
            string password;
            userName = "me@myWebsite.com";
            password = "myPassword";
            SforceService SfdcBinding = null;
            LoginResult CurrentLoginResult = null;
            SfdcBinding = new SforceService();
            try
            {
                CurrentLoginResult = SfdcBinding.login(userName, password);
            }
            catch (System.Web.Services.Protocols.SoapException e)
            {
                // This is likley to be caused by bad username or password
                SfdcBinding = null;
                throw (e);
            }
            catch (Exception e)
            {
                // This is something else, probably comminication
                SfdcBinding = null;
                throw (e);
            }
        }

错误指出我需要一个安全令牌,但文档似乎从未提及,我也不确定如何获得。

可以';t连接到C#中的SalesForce

我要做的(文档中没有)是转到这里:

https://na15.salesforce.com/_ui/system/security/ResetApiTokenConfirm?retURL=%2Fui%2Fsetup%2FSetup%3Fsetupid%3DPersonalInfo&setupid=ResetApiToken

然后,重置我的代币。然后,将其附加到我的密码末尾,如:

如果您的密码=";mypassword";

并且您的安全令牌=";XXXXXXXXXX";

您必须输入";我的密码XXXXXXXXXX";代替您的密码

参考。http://docs.servicerocket.com/pages/viewpage.action?pageId=83099770

对于这样的SOAP API,您需要首先通过提供用户名和密码来对服务进行身份验证。他们的响应应该返回一个在一段时间内有效的授权令牌。然后,在随后的通信中,您将此令牌传递给API,以便它知道您是谁。

获取授权令牌:

SforceService SfdcBinding = null;
LoginResult CurrentLoginResult = null;
SfdcBinding = new SforceService();
try 
{
   CurrentLoginResult = SfdcBinding.login(userName, password);
}
catch (System.Web.Services.Protocols.SoapException e) 
{
   // This is likely to be caused by bad username or password
   SfdcBinding = null;
   throw (e);
}
catch (Exception e) 
{
   // This is something else, probably communication
   SfdcBinding = null;
   throw (e);
}

设置会话:

//Change the binding to the new endpoint
SfdcBinding.Url = CurrentLoginResult.serverUrl;
//Create a new session header object and set the session id to that returned by the login
SfdcBinding.SessionHeaderValue = new SessionHeader();
SfdcBinding.SessionHeaderValue.sessionId = CurrentLoginResult.sessionId;

执行查询:

QueryResult queryResult = null;
String SOQL = "select FirstName, LastName, Phone from Lead where email = 'john.smith@salesforce.com'";
queryResult = SfdcBinding.query(SOQL);