1

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

但是,我收到此错误:

LOGIN_MUST_USE_SECURITY_TOKEN:用户名、密码、安全令牌无效;或用户被锁定。你在一个新的地方吗?通过桌面客户端或 API 从您公司的受信任网络外部访问 Salesforce 时,您必须在密码中添加安全令牌才能登录。要接收新的安全令牌,请登录 salesforce.com,网址为http://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);
            }

        }

错误表明我需要一个安全令牌,但文档似乎从未提及它,我不知道如何获得一个。

4

2 回答 2

9

我必须做的(不在文档中)是到这里:

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

并且,重置我的令牌。然后,将其附加到我的密码末尾,例如:

如果您的密码 = "mypassword"

而您的安全令牌 = "XXXXXXXXXX"

您必须输入“mypasswordXXXXXXXXXX”代替您的密码

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

于 2013-07-26T16:53:27.923 回答
2

使用这样的 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);
于 2013-07-26T16:44:04.323 回答