使用 Salesforce.com SOAP API 实现用户登录

Salesforce.com 作为一个CRM综合应用平台, 允许客户端以多种方式来接入和访问平台应用. Salesforce.com SOAP API 提供了一个基于 SOAP (Simple Object Access protocol) 协议的应用接口以支持第三方代码.

本文介绍如何使用Salesforce.com SOAP API 来实现基本的用户登录操作.

首先, 从Salesforce.com平台下载Web Service WSDL

使用浏览器登录Salesforce.com后选择Build - Develop - API, 选择Force.com Enterprise WSDL 或  Force.com Partner WSDL.



其次, 使用下载的WSDL文件生成 SOAP客户端,

从https://code.google.com/p/sfdc-wsc/下载Force.com Web Service Connector (WSC), 并运行以下命令

java -classpath <path to>/wsc-23.jar com.sforce.ws.tools.wsdlc <path to wsdl> <output jar file>

将生成的SOAP客户端jar文件加入Java项目的classpath

以下代码使用基于 Partner WSDL的SOAP客户端来登录Salesforce.com.

private void runPartnerLogin(String username, String password,
		String securityToken, boolean useProxy, String proxyHost,
		int proxyPort, String proxyUserName, String proxyPassword) {

	try {
		String partnerAuthEndPoint = "https://login.salesforce.com/services/Soap/u/32.0/";
		
		ConnectorConfig config = new ConnectorConfig();

		config.setUsername(username);
		config.setPassword(password + securityToken);
		config.setAuthEndpoint(partnerAuthEndPoint);

		if (useProxy) {
			config.setProxy(proxyHost, proxyPort);
			config.setProxyUsername(proxyUserName);
			config.setProxyPassword(proxyPassword);
		}

		PartnerConnection connection = new PartnerConnection(config);

		// Print user and session info
		com.sforce.soap.partner.GetUserInfoResult userInfo = connection
				.getUserInfo();
		System.out.println("UserID: " + userInfo.getUserId());
		System.out.println("User Full Name: " + userInfo.getUserFullName());
		System.out.println("User Email: " + userInfo.getUserEmail());
		System.out.println();
		System.out.println("SessionID: " + config.getSessionId());
		System.out.println("Auth End Point: " + config.getAuthEndpoint());
		System.out.println("Service End Point: "
				+ config.getServiceEndpoint());
		System.out.println();

	} catch (ConnectionException ce) {
		ce.printStackTrace();
	}
}

private void runEnterpriseLogin(String username, String password,
		String securityToken, boolean useProxy, String proxyHost,
		int proxyPort, String proxyUserName, String proxyPassword) {
	
	try {
		String enterpriseAuthEndPoint = "https://login.salesforce.com/services/Soap/c/32.0/";
		
		ConnectorConfig config = new ConnectorConfig();
		
		config.setUsername(username);
		config.setPassword(password + securityToken);
		config.setAuthEndpoint(enterpriseAuthEndPoint);
		
		config.setProxy(proxyHost, proxyPort);
		config.setProxyUsername(proxyUserName);
		config.setProxyPassword(proxyPassword);
		
		
		EnterpriseConnection connection = new EnterpriseConnection(config);

		// Print user and session info
		com.sforce.soap.enterprise.GetUserInfoResult userInfo = connection.getUserInfo();
		System.out.println("UserID: " + userInfo.getUserId());
		System.out.println("User Full Name: " + userInfo.getUserFullName());
		System.out.println("User Email: " + userInfo.getUserEmail());
		System.out.println();
		System.out.println("SessionID: " + config.getSessionId());
		System.out.println("Auth End Point: " + config.getAuthEndpoint());
		System.out.println("Service End Point: "
				+ config.getServiceEndpoint());
		System.out.println();
		
	} catch (ConnectionException ce) {
		ce.printStackTrace();
	}
}



代码中所需的Security Token可以从My Settings -  Personal  -  Reset My Security Token 界面中获取



已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页